Exemple #1
0
 public static UnsafeNativeMethods.ITypeInfo FindTypeInfo(object obj, bool wantCoClass)
 {
     UnsafeNativeMethods.ITypeInfo classInfo = null;
     for (int i = 0; (classInfo == null) && (i < 2); i++)
     {
         if (wantCoClass == (i == 0))
         {
             if (obj is System.Windows.Forms.NativeMethods.IProvideClassInfo)
             {
                 System.Windows.Forms.NativeMethods.IProvideClassInfo info2 = (System.Windows.Forms.NativeMethods.IProvideClassInfo)obj;
                 try
                 {
                     classInfo = info2.GetClassInfo();
                 }
                 catch
                 {
                 }
             }
         }
         else if (obj is UnsafeNativeMethods.IDispatch)
         {
             UnsafeNativeMethods.IDispatch dispatch = (UnsafeNativeMethods.IDispatch)obj;
             try
             {
                 classInfo = dispatch.GetTypeInfo(0, SafeNativeMethods.GetThreadLCID());
             }
             catch
             {
             }
         }
     }
     return(classInfo);
 }
        /// <include file='doc\COM2TypeInfoProcessor.uex' path='docs/doc[@for="Com2TypeInfoProcessor.FindTypeInfo"]/*' />
        /// <devdoc>
        /// Given an Object, this attempts to locate its type ifo
        /// </devdoc>
        public static UnsafeNativeMethods.ITypeInfo FindTypeInfo(object obj, bool wantCoClass)
        {
            UnsafeNativeMethods.ITypeInfo pTypeInfo = null;

            // this is kind of odd.  What's going on here is that
            // if we want the CoClass (e.g. for the interface name),
            // we need to look for IProvideClassInfo first, then
            // look for the typeinfo from the IDispatch.
            // In the case of many OleAut32 operations, the CoClass
            // doesn't have the interface members on it, although
            // in the shell it usually does, so
            // we need to re-order the lookup if we _actually_ want
            // the CoClass if it's available.
            //

            for (int i = 0; pTypeInfo == null && i < 2; i++)
            {
                if (wantCoClass == (i == 0))
                {
                    if (obj is NativeMethods.IProvideClassInfo)
                    {
                        NativeMethods.IProvideClassInfo pProvideClassInfo = (NativeMethods.IProvideClassInfo)obj;
                        try {
                            pTypeInfo = pProvideClassInfo.GetClassInfo();
                        }
                        catch {
                        }
                    }
                }
                else
                {
                    if (obj is UnsafeNativeMethods.IDispatch)
                    {
                        UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)obj;
                        try {
                            pTypeInfo = iDispatch.GetTypeInfo(0, SafeNativeMethods.GetThreadLCID());
                        }
                        catch {
                        }
                    }
                }
            }
            return(pTypeInfo);
        }