力蒙上了COM对象的接口接口、对象、COM

2023-09-06 11:32:44 作者:鬼毅

我使用的是COM API访问某一类,但应用程序开发人员还没有充分暴露了类COM。所以,当我键入 shape.Custom.Cells VS抱怨细胞不存在..唧唧歪歪。

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..".

但是,当我调试我的code和开拓对象VS调试器中,它具有属性! ..正是属性我试图访问!

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; }

}

和铸造COM对象到我的界面......

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

 CorelTableShape table = (CorelTableShape)shape.Custom;

...触发InvalidCastException的!有没有办法在运行时访问这些属性?

... 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.

您可以尝试使用动态代替。你会失去IntelliSense支持,但我认为你能忍受这一点。

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