Force cast a COM object to an interface

蓝咒 提交于 2019-12-24 11:59: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!

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?


回答1:


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.



来源:https://stackoverflow.com/questions/20819281/force-cast-a-com-object-to-an-interface

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