C# Casting system.__comobject to class type

Posted by ijrufus on Stack Overflow See other posts from Stack Overflow or by ijrufus
Published on 2013-08-02T15:33:11Z Indexed on 2013/08/02 15:36 UTC
Read the original article Hit count: 167

Filed under:
|
|
|

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
> 'anaplan.Utility.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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel