Force cast a COM object to an interface

2019-08-07 03:17发布

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条回答
ら.Afraid
2楼-- · 2019-08-07 03:42

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.

查看更多
登录 后发表回答