在與COM對(duì)象交互的時(shí)候有的時(shí)候我們得到一個(gè)對(duì)象,我們想知道它的類型,可以使用Object.GetType()方法得到的類型卻是System.__ComObject,因?yàn)镾ystem.__ComObject是代表所有COM對(duì)象的,但是它對(duì)我們來(lái)說(shuō)是沒(méi)有任何意義的。如果想得到System.__ComObject的真正類型只要使用Microsoft.VisualBasic.Information.TypeName(objWindow.Object)就可以了,如果是非VB.net工程需要引用Microsoft.VisualBasic.dll 才能保證編譯通過(guò)。
12月6日添加說(shuō)明:
經(jīng)過(guò)反編譯TypeName方法,發(fā)現(xiàn)其核心實(shí)現(xiàn)為:
UnsafeNativeMethods.
ITypeInfo pTypeInfo =
null;
string pBstrName =
null;
string pBstrDocString =
null;
string pBstrHelpFile =
null;
UnsafeNativeMethods.
IDispatch dispatch =
VarName as UnsafeNativeMethods.
IDispatch;
if (((
dispatch !=
null) && (
dispatch.
GetTypeInfo(
0,
0x409,
out pTypeInfo) >=
0)) && (
pTypeInfo.
GetDocumentation(
-1,
out pBstrName,
out pBstrDocString,
out num,
out pBstrHelpFile) >=
0))
{
str5 =
pBstrName;
}
和猜想的一致,它確實(shí)是通過(guò)
IDispatch接口來(lái)完成的(呵呵,貌似也只有這一種方式)