Force cast a COM object to an interface

489 views Asked by At

I'm using a COM API to access a certain class, but the app developer has not fully exposed the class to COM. So when I type shape.Custom.Cells VS complains "Cells does not exist .. bla bla bla..".

But, when I debug my code, and open up the object inside the VS debugger, it HAS properties! .. exactly the properties I'm trying to access!

enter image description here

So I'm trying to access these properties with a handwritten interface:

internal interface CorelTableShape {

object Cells { get; }
object Borders { get; }
object Columns { get; }
object Rows { get; }
object Selection { get; }

}

And casting the COM object to my interface ....

 CorelTableShape table = (CorelTableShape)shape.Custom;

... triggers a InvalidCastException! Is there any way to access these properties at runtime?

err

1

There are 1 answers

0
MarcinJuraszek On BEST ANSWER

You can't cast an object to an interface it does not implement. So you can't create your own interface and try to cast an object to it.

You can try using dynamic instead. You'll loose IntelliSense support, but I think you can live with this.