C# - 如何在运行时的COM组件类测试是用来实例化一个接口? [英] C# - How to test at runtime which COM coclass was used to instantiate an interface?
问题描述
假设你有一个COM接口 ICOMInterface
由组件类实施 Coclass1
和 Coclass2
。无论这些组件类都有自己的接口(为简单起见,并说明我的问题)。
Suppose you have a COM interface ICOMInterface
that is implemented by coclasses Coclass1
and Coclass2
. Neither of these coclasses have interfaces of their own (for simplicity's sake and to illustrate my issue).
在C#中,你可以从一个组件类创建一个COM接口的实例像这样:
In C#, you can create an instance of a COM interface from a coclass like so:
ICOMInterface myComInterface = new Coclass1();
现在,你怎么能确定是否 myComInterface
由 Coclass1
或 Coclass2
?
Now, how can you determine whether myComInterface
was instantiated by Coclass1
or Coclass2
?
使用实例是语句就像如下总是返回true,因此是无用的用于这一目的。
Using the "is" statement like follows always returns true, and as such is useless for this purpose.
Debug.WriteLine(myComInterface is Coclass1) // writes "True"
Debug.WriteLine(myComInterface is Coclass2) // writes "True"
这将工作,如果我在测试接口,而不是组件类,但这些组件类没有一个比他们都实现其他接口, ICOMInterface
。
This would work if I was testing interfaces, not coclasses, but these coclasses do not have interfaces other than the one they both implement, ICOMInterface
.
我希望有一个简单的答案,这是我可以俯瞰比较通用的情况下,否则,如果需要的话我可以发布更具体的细节。
I am hoping that there is a simple answer to this rather generic scenario that I am overlooking, otherwise I can post more specific details if required.
感谢您的帮助。
推荐答案
如果COM对象实现的 的IPersist接口,你可以通过它的IPersist CLSID: :的GetClassID()
方法。这可能是所有你需要知道你正在使用什么课。您也可以通过WinAPI的获得人类可读的进程id ProgIDFromCLSID()
法
If the COM object implements the IPersist interface, you can get its CLSID through the IPersist::GetClassID()
method. This may be all you need to know what class you're working with. You can also get the human-readable ProgID through the WinAPI ProgIDFromCLSID()
method.
在这里看到:<一HREF =http://stackoverflow.com/questions/1253368/c-get-progid-from-com-object> http://stackoverflow.com/questions/1253368/c-get-progid-from-com-反对
这篇关于C# - 如何在运行时的COM组件类测试是用来实例化一个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!