如何在 C# 中实例化 IDispatchEx?
这是我的第一个问题! 我想实例化一个 COM 对象并将其强制转换为 IDispatchEx,以便我可以枚举其成员。下面是一个示例:
Type _COMType = System.Type.GetTypeFromProgID("Scripting.FileSystemObject"); var _COMObject = (IDispatchEx)Activator.CreateInstance(_COMType);
我的 IDispatchEx 与此网站上的相同 (不是我的网站),除了 GetNextDispID 和GetMemberName 返回一个 int (我希望将其用于 HRESULT,如所述 MSDN)。
上面的例子不起作用。有没有办法像从 Active Scripting 转换到 IDispatchEx 接口一样实例化 COM 对象?
感谢您的任何和所有帮助/建议!
this is my first SO question!
I would like to instantiate a COM object and cast it to IDispatchEx so that I can enumerate its members. Here is an example:
Type _COMType = System.Type.GetTypeFromProgID("Scripting.FileSystemObject"); var _COMObject = (IDispatchEx)Activator.CreateInstance(_COMType);
My IDispatchEx is identical to the one on this website (not my website), except that GetNextDispID and GetMemberName return an int (which I wish to use for HRESULT as described on MSDN).
The example above does not work. Is there any way to instantiate COM objects as you would from Active Scripting cast to the IDispatchEx interface?
Thanks for any and all help/suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到的异常消息非常清楚,Scripting.FileSystemObject 根本没有实现 IDispatchEx 接口。仅 IDispatch。这工作得很好:
你已经完成了,你不能强制 COM 组件类实现接口。我不希望有很多 COM 类来实现它,IDispatchEx 相当晦涩难懂。它适合 JScript 模式。
The exception message you get is as clear as a bell, the Scripting.FileSystemObject simply doesn't implement the IDispatchEx interface. Only IDispatch. This works fine:
You're done, you cannot force a COM coclass to implement an interface. I would not expect very many COM classes to implement it, IDispatchEx is pretty obscure. It fits the JScript mold.
如果您想在 C# 中使用它,您似乎需要自己定义此接口(源代码)
此帖子可能相关 - 似乎有人已经找到了 IDispatchEx 的现有实现可供使用。
It seems that you will need to define this interface yourself if you wish to use it in C# (source)
This thread may be relevant - it seems that someone has found an existing implementation of IDispatchEx to use.