C# - 如何在运行时测试COM coclass用于实例化接口? [英] C# - How to test at runtime which COM coclass was used to instantiate an interface?
问题描述
假设您有一个COM接口 ICOMInterface
,由coclasses实现 Coclass1
和 Coclass2
。这些coclass没有自己的接口(为了简单起见,并说明我的问题)。
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接口的实例从coclass像这样:
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
?
is语句如下总是返回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"
$ b b
这将工作,如果我是测试接口,而不是coclasses,但这些coclass没有除他们两者实现的接口, 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 ProgIDFromCLSID()
方法获取人类可读的ProgID。
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> C#从COM对象获取progID
See here: C# Get progID from COM object
这篇关于C# - 如何在运行时测试COM coclass用于实例化接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!