力蒙上了COM对象的接口 [英] Force cast a COM object to an interface

查看:129
本文介绍了力蒙上了COM对象的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是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.

这篇关于力蒙上了COM对象的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆