强制转换COM对象到接口 [英] Force cast a COM object to an interface
问题描述
我使用COM API访问某个类,但应用程序开发人员尚未将该类完全公开到COM。所以当我输入 shape.Custom.Cells
VS抱怨Cells does not exist .. bla bla bla ..。
但是,当我调试我的代码,并打开VS调试器内的对象,它的HAS属性!
<$>
<$> p $ p>
内部接口CorelTableShape {
object Cells {get; }
object Borders {get; }
object Columns {get; }
object Rows {get; }
object Selection {get; }
}
并将COM物件投放到我的介面... 。
CorelTableShape table =(CorelTableShape)shape.Custom;
...触发InvalidCastException!在运行时有没有办法访问这些属性?
您不能将对象投射到未实现的接口。因此,您无法创建自己的界面,并尝试向其投射对象。
您可以尝试使用 dynamic
改为。您将放弃IntelliSense支持,但我认为您可以忍受这一点。
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?
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.
这篇关于强制转换COM对象到接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!