C# Casting system.__comobject to class type

有些话、适合烂在心里 提交于 2019-12-24 13:25:38

问题


I have an Excel Add-In that I'm currently trying to set up a unit test framework for. For the unit tests I've followed this guide: http://blogs.msdn.com/b/varsha/archive/2010/08/17/writing-automated-test-cases-for-vsto-application.aspx

It seems to work fine, until I want to return a class object from my interface. Specifying the class object as the return type throws a "return argument has an invalid type" exception when calling the method. Changing the return type from the class to an object allows me to call the method and get the object, but now I'm unable to cast it as the class and use it as intended, getting this exception message when I try:

> Unable to cast COM object of type 'System.__ComObject' to class type
> 'XYCoordinates'. Instances of types that represent COM
> components cannot be cast to types that do not represent COM
> components; however they can be cast to interfaces as long as the
> underlying COM component supports QueryInterface calls for the IID of
> the interface.

I've retrieved the Type name using VisualBasic.Information.TypeName and it's showing it as the class I expect.

Is there any way to get the comobject cast back to the class? Or another way to access the properties it has? Or am I just being a bit stupid here?

来源:https://stackoverflow.com/questions/18020815/c-sharp-casting-system-comobject-to-class-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!