【转】获取System.__ComObject的真正类型

在与COM对象交互的时候有的时候我们得到一个对象,我们想知道它的类型,可以使用Object.GetType()方法得到的类型却是System.__ComObject,因为System.__ComObject是代表所有COM对象的,但是它对我们来说是没有任何意义的。如果想得到System.__ComObject的真正类型只要使用Microsoft.VisualBasic.Information.TypeName(objWindow.Object)就可以了,如果是非VB.net工程需要引用Microsoft.VisualBasic.dll 才能保证编译通过。
12月6日添加说明:
经过反编译TypeName方法,发现其核心实现为:
    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;
    }

和猜想的一致,它确实是通过IDispatch接口来完成的(呵呵,貌似也只有这一种方式)

 

原文地址:http://www.blogjava.net/huanzhugege/archive/2007/12/05/165462.html

posted @ 2010-03-24 20:17  画竹  阅读(1101)  评论(0编辑  收藏  举报