Пример #1
0
        protected override void OnDefineType()
        {
            string classInterfaceName = m_info.GetUniqueManagedName(m_coclassTypeInfo, ConvType.ClassInterface);

            Type defaultInterfaceType       = null;
            Type defaultSourceInterfaceType = null;

            m_convInterface       = null;
            m_convSourceInterface = null;

            //
            // Convert default interface
            //
            if (m_defaultInterfaceTypeInfo != null)
            {
                m_convInterface = (IConvInterface)m_info.GetTypeRef(ConvType.Interface, m_defaultInterfaceTypeInfo);
                // Don't create the interface because we haven't associated the default interface with the class interface yet
                // We don't want to create anything in the "Define" stage
                //m_convInterface.Create();
                defaultInterfaceType = m_convInterface.ManagedType;
            }

            //
            // Convert default source interface
            //
            if (m_defaultSourceInterfaceTypeInfo != null)
            {
                m_convSourceInterface = (IConvInterface)m_info.GetTypeRef(ConvType.Interface, m_defaultSourceInterfaceTypeInfo);
                // Don't create the interface because we haven't associated the default interface with the class interface yet
                // We don't want to create anything in the "Define" stage
                // m_convSourceInterface.Create();
                Type sourceInterfaceType = m_convSourceInterface.RealManagedType;
                IConvEventInterface convEventInterface = m_convSourceInterface.DefineEventInterface();
                // Don't create the interface because we haven't associated the default interface with the class interface yet
                // We don't want to create anything in the "Define" stage
                // convEventInterface.Create();
                defaultSourceInterfaceType = m_convSourceInterface.EventInterface.ManagedType;
            }

            //
            // Prepare list of implemented interfaces
            //
            List <Type> implTypes = new List <Type>();

            if (defaultInterfaceType != null)
            {
                implTypes.Add(defaultInterfaceType);
            }
            if (defaultSourceInterfaceType != null)
            {
                implTypes.Add(defaultSourceInterfaceType);
            }

            // Create the class interface
            m_typeBuilder = m_info.ModuleBuilder.DefineType(
                classInterfaceName,
                TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Import,
                null,
                implTypes.ToArray());

            // Link to it so that ManagedType will return the class interface while GetWrappedInterfaceType will return the
            // real interface
            // This must be done before creating the coclass because coclass needs this information
            // Only do so when the default interface is exclusively belongs to one coclass

            if (m_convInterface != null && m_isExclusive)
            {
                // Check if the default interface -> class interface relationship exists in the default
                // interface's type lib. That means we only need to check if the default interface and
                // the coclass are in the same type library.
                TypeLib typeLib = m_convInterface.RefTypeInfo.GetContainingTypeLib();
                Guid    libIdOfDefaultInterface;
                using (TypeLibAttr libAttr = typeLib.GetLibAttr())
                {
                    libIdOfDefaultInterface = libAttr.guid;
                }
                Guid    libIdOfCoclass;
                TypeLib coclassTypeLib = m_coclassTypeInfo.GetContainingTypeLib();
                using (TypeLibAttr libAttr = coclassTypeLib.GetLibAttr())
                {
                    libIdOfCoclass = libAttr.guid;
                }
                if (libIdOfDefaultInterface.Equals(libIdOfCoclass))
                {
                    m_convInterface.AssociateWithExclusiveClassInterface(this as IConvClassInterface);
                }
            }

            // Emit GuidAttribute, which is the same as the default interface, if it exists
            // If there is no default Interface here, and the coclass implements IDispatch or IUnknown as non-source
            // interface, we use the IDispatch or IUnknown's guid.
            if (defaultInterfaceType != null)
            {
                ConvCommon.DefineGuid(m_convInterface.RefTypeInfo, m_convInterface.RefNonAliasedTypeInfo, m_typeBuilder);
            }
            else
            {
                TypeInfo ImplementedIDispatchOrIUnknownTypeInfo = null;
                using (TypeAttr attr = m_coclassTypeInfo.GetTypeAttr())
                {
                    for (int m = 0; m < attr.cImplTypes; ++m)
                    {
                        TypeLibTypes.Interop.IMPLTYPEFLAGS flags = m_coclassTypeInfo.GetImplTypeFlags(m);
                        bool bDefault = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) != 0;
                        bool bSource  = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0;

                        TypeInfo typeImpl = m_coclassTypeInfo.GetRefType(m);
                        using (TypeAttr attrImpl = typeImpl.GetTypeAttr())
                        {
                            if (attrImpl.Guid == WellKnownGuids.IID_IDispatch ||
                                attrImpl.Guid == WellKnownGuids.IID_IUnknown)
                            {
                                // If more than one IDispatch or IUnknown exist, we will pick the default one;
                                // If none of them is with the default flag, pick the first one.
                                if (!bSource && (bDefault || ImplementedIDispatchOrIUnknownTypeInfo == null))
                                {
                                    ImplementedIDispatchOrIUnknownTypeInfo = typeImpl;
                                }
                            }
                        }
                    }
                }
                if (ImplementedIDispatchOrIUnknownTypeInfo != null)
                {
                    ConvCommon.DefineGuid(ImplementedIDispatchOrIUnknownTypeInfo,
                                          ImplementedIDispatchOrIUnknownTypeInfo, m_typeBuilder);
                }
            }

            // Make sure we know about the class interface before we go to define the coclass in the next statement
            m_info.RegisterType(m_typeBuilder, this);
            m_info.AddToSymbolTable(m_coclassTypeInfo, ConvType.ClassInterface, this);

            // Handle [CoClass(typeof(...))]
            Type            typeRefCoClass       = m_info.GetTypeRef(ConvType.CoClass, m_coclassTypeInfo).ManagedType;
            ConstructorInfo ctorCoClassAttribute = typeof(CoClassAttribute).GetConstructor(
                new Type[] { typeof(Type) });
            // For back compatibility, use full name to create CoClassAttribute, instead of assembly qualified name.
            CustomAttributeBlobBuilder blobBuilder = new CustomAttributeBlobBuilder();

            blobBuilder.AddFixedArg(typeRefCoClass.FullName);
            m_typeBuilder.SetCustomAttribute(ctorCoClassAttribute, blobBuilder.GetBlob());
        }
Пример #2
0
        protected override void OnDefineType()
        {
            TypeInfo typeInfo = RefNonAliasedTypeInfo;

            using (TypeAttr attr = typeInfo.GetTypeAttr())
            {
                string name = m_info.GetUniqueManagedName(RefTypeInfo, ConvType.CoClass);

                //
                // Collect information for a list of interfaces & event interface types
                //
                List <Type> intfList                 = new List <Type>(); // interface list
                List <Type> eventIntfList            = new List <Type>(); // event interface list
                TypeInfo    defaultInterfaceTypeInfo = null;

                int nCount = attr.cImplTypes;

                string sourceInterfaceNames  = String.Empty;
                bool   hasDefaultInterface   = false;
                bool   implementsIEnumerable = ConvCommon.ExplicitlyImplementsIEnumerable(typeInfo, attr);

                for (int n = 0; n < nCount; ++n)
                {
                    TypeLibTypes.Interop.IMPLTYPEFLAGS flags = typeInfo.GetImplTypeFlags(n);
                    bool isDefault = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) != 0;
                    bool isSource  = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0;

                    TypeInfo typeImpl = typeInfo.GetRefType(n);

                    using (TypeAttr attrImpl = typeImpl.GetTypeAttr())
                    {
                        // Skip IUnknown & IDispatch
                        if (attrImpl.Guid == WellKnownGuids.IID_IDispatch || attrImpl.Guid == WellKnownGuids.IID_IUnknown)
                        {
                            continue;
                        }

                        // Skip non-dispatch interfaces that doesn't derive from IUnknown
                        if (!attrImpl.IsDispatch && !ConvCommon.IsDerivedFromIUnknown(typeImpl))
                        {
                            continue;
                        }

                        IConvInterface convInterface = (IConvInterface)m_info.GetTypeRef(ConvType.Interface, typeImpl);

                        ConvCommon.ThrowIfImplementingExportedClassInterface(RefTypeInfo, convInterface);

                        // For source interfaces, try create the event interface
                        // Could be already created if it is the default source interface
                        if (isSource)
                        {
                            convInterface.DefineEventInterface();
                        }

                        // Use the RealManagedType (avoid getting the class interface)
                        Type typeRef = convInterface.RealManagedType;

                        // Append the source interface name to the list for the ComSourceInterfacesAttribute
                        if (isSource)
                        {
                            string interfaceName;
                            if (convInterface.ConvScope == ConvScope.External)
                            {
                                interfaceName = typeRef.AssemblyQualifiedName + "\0";
                            }
                            else
                            {
                                interfaceName = typeRef.FullName + "\0";
                            }

                            // Insert default source interface to the beginning
                            if (isDefault)
                            {
                                sourceInterfaceNames = interfaceName + sourceInterfaceNames;
                            }
                            else
                            {
                                sourceInterfaceNames = sourceInterfaceNames + interfaceName;
                            }
                        }

                        if (isDefault)
                        {
                            // Add the real interface first
                            if (isSource)
                            {
                                // For source interface, use the event interface instead
                                // Insert to the beginning
                                eventIntfList.Insert(0, convInterface.EventInterface.ManagedType);
                            }
                            else
                            {
                                m_defaultInterface = convInterface;

                                // Insert to the beginning
                                intfList.Insert(0, typeRef);
                                hasDefaultInterface      = true;
                                defaultInterfaceTypeInfo = typeImpl;
                            }
                        }
                        else
                        {
                            if (isSource)
                            {
                                // For source interface, add the event interface instead
                                eventIntfList.Add(convInterface.EventInterface.ManagedType);
                            }
                            else
                            {
                                if (m_defaultInterface == null)
                                {
                                    m_defaultInterface       = convInterface;
                                    defaultInterfaceTypeInfo = typeImpl;
                                }

                                intfList.Add(typeRef);
                            }
                        }
                    }
                }

                //
                // Get class interface
                //
                m_classInterface = m_info.GetTypeRef(ConvType.ClassInterface, RefTypeInfo) as IConvClassInterface;
                if (m_classInterface == null)
                {
                    throw new TlbImpInvalidTypeConversionException(RefTypeInfo);
                }

                //
                // Build implemented type list in a specific order
                //
                List <Type> implTypeList = new List <Type>();
                if (hasDefaultInterface)
                {
                    implTypeList.Add(intfList[0]);
                    intfList.RemoveAt(0);

                    implTypeList.Add(m_classInterface.ManagedType);
                }
                else
                {
                    implTypeList.Add(m_classInterface.ManagedType);
                    if (intfList.Count > 0)
                    {
                        implTypeList.Add(intfList[0]);
                        intfList.RemoveAt(0);
                    }
                }

                if (eventIntfList.Count > 0)
                {
                    implTypeList.Add(eventIntfList[0]);
                    eventIntfList.RemoveAt(0);
                }

                implTypeList.AddRange(intfList);
                implTypeList.AddRange(eventIntfList);

                // Check to see if the default interface has a member with a DISPID of DISPID_NEWENUM.
                if (defaultInterfaceTypeInfo != null)
                {
                    if (!implementsIEnumerable && ConvCommon.HasNewEnumMember(m_info, defaultInterfaceTypeInfo, name))
                    {
                        implTypeList.Add(typeof(System.Collections.IEnumerable));
                    }
                }

                // Check to see if the IEnumerable Custom Value exists on the CoClass if doesn't inherit from IEnumerable yet
                if (!implTypeList.Contains(typeof(System.Collections.IEnumerable)))
                {
                    if (ConvCommon.HasForceIEnumerableCustomAttribute(typeInfo))
                    {
                        implTypeList.Add(typeof(System.Collections.IEnumerable));
                    }
                }

                // Define the type
                m_typeBuilder = m_info.ModuleBuilder.DefineType(name,
                                                                TypeAttributes.Public | TypeAttributes.Import,
                                                                typeof(Object),
                                                                implTypeList.ToArray());

                // Handle [Guid(...)] custom attribute
                ConvCommon.DefineGuid(RefTypeInfo, RefNonAliasedTypeInfo, m_typeBuilder);

                // Handle [ClassInterface(ClassInterfaceType.None)]
                m_typeBuilder.SetCustomAttribute(CustomAttributeHelper.GetBuilderForClassInterface(ClassInterfaceType.None));

                // Handle [TypeLibType(...)] if evaluate to non-0
                using (TypeAttr refTypeAttr = RefTypeInfo.GetTypeAttr())
                {
                    if (refTypeAttr.wTypeFlags != 0)
                    {
                        m_typeBuilder.SetCustomAttribute(CustomAttributeHelper.GetBuilderForTypeLibType((TypeLibTypeFlags)refTypeAttr.wTypeFlags));
                    }
                }

                // Handle [ComSourceInterfacesAttribute]
                if (sourceInterfaceNames != String.Empty)
                {
                    sourceInterfaceNames += "\0";
                    m_typeBuilder.SetCustomAttribute(CustomAttributeHelper.GetBuilderForComSourceInterfaces(sourceInterfaceNames));
                }

                // Add to symbol table automatically
                m_info.AddToSymbolTable(RefTypeInfo, ConvType.CoClass, this);

                // Register type
                m_info.RegisterType(m_typeBuilder, this);
            }
        }
Пример #3
0
        /// <summary>
        /// Collect information
        /// </summary>
        private void Collect()
        {
            // Remember all the interface name to coclass name mapping
            Hashtable interfaceToCoClassMapping = new Hashtable();

            //
            // For every coclass
            //
            int nCount = m_typeLib.GetTypeInfoCount();

            for (int n = 0; n < nCount; ++n)
            {
                TypeInfo type = m_typeLib.GetTypeInfo(n);
                //
                // Walk the list of implemented interfaces
                //
                using (TypeAttr attr = type.GetTypeAttr())
                {
                    if (attr.typekind == TypeLibTypes.Interop.TYPEKIND.TKIND_COCLASS)
                    {
                        DefaultInterfaceInfo defaultInterfaceInfo = new DefaultInterfaceInfo();
                        defaultInterfaceInfo.coclass     = type;
                        defaultInterfaceInfo.coclassName = type.GetDocumentation();

                        for (int m = 0; m < attr.cImplTypes; ++m)
                        {
                            TypeLibTypes.Interop.IMPLTYPEFLAGS flags = type.GetImplTypeFlags(m);
                            bool bDefault = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) != 0;

                            // For invalid default interfaces, such as
                            // coclass MyObj
                            // {
                            //     [default] interface IUnknown;
                            //     interface IA;
                            // }
                            // to use the first valid interface, which is IA;
                            // if (!bDefault) continue;

                            bool bSource = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0;

                            TypeInfo typeImpl = type.GetRefType(m);

                            using (TypeAttr attrImpl = typeImpl.GetTypeAttr())
                            {
                                // Skip IUnknown & IDispatch
                                if (attrImpl.Guid == WellKnownGuids.IID_IDispatch ||
                                    attrImpl.Guid == WellKnownGuids.IID_IUnknown)
                                {
                                    continue;
                                }

                                // Skip non-dispatch interfaces that doesn't derive from IUnknown
                                if (!attrImpl.IsDispatch && !ConvCommon.IsDerivedFromIUnknown(typeImpl))
                                {
                                    continue;
                                }

                                string name = typeImpl.GetDocumentation();

                                if (bSource)
                                {
                                    // Default source interface
                                    if (bDefault ||                                             // If explicitly stated as default, use that
                                        defaultInterfaceInfo.defaultSourceInterface == null)    // otherwise, try to use the first one
                                    {
                                        defaultInterfaceInfo.defaultSourceInterface     = typeImpl;
                                        defaultInterfaceInfo.defaultSourceInterfaceName = name;
                                    }
                                }
                                else
                                {
                                    // Default interface
                                    if (bDefault ||                                     // If explicitly stated as default, use that
                                        defaultInterfaceInfo.defaultInterface == null)  // otherwise, try to use the first one
                                    {
                                        defaultInterfaceInfo.defaultInterface     = typeImpl;
                                        defaultInterfaceInfo.defaultInterfaceName = name;
                                    }
                                }
                            }
                        }

                        //
                        // Walk through the list of implemented interfaces again. This time we remember all the implemented interfaces (including base)
                        //
                        for (int m = 0; m < attr.cImplTypes; ++m)
                        {
                            TypeInfo typeImpl = type.GetRefType(m);
                            string   name     = typeImpl.GetDocumentation();

                            while (typeImpl != null)
                            {
                                // If we arleady seen this interface
                                if (interfaceToCoClassMapping.Contains(name))
                                {
                                    // and if it is for a different interface
                                    if ((string)interfaceToCoClassMapping[name] != defaultInterfaceInfo.coclassName)
                                    {
                                        // Set the name to null so that we know we've seen other interfaces
                                        interfaceToCoClassMapping[name] = null;
                                    }
                                }
                                else
                                {
                                    interfaceToCoClassMapping.Add(name, defaultInterfaceInfo.coclassName);
                                }

                                TypeAttr attrImpl = typeImpl.GetTypeAttr();
                                if (attrImpl.cImplTypes == 1)
                                {
                                    typeImpl = typeImpl.GetRefType(0);
                                }
                                else
                                {
                                    typeImpl = null;
                                }
                            }
                        }

                        // We do allow coclass that doesn't have any 'valid' default interfaces to have a class interface
                        // For example,
                        // coclass MyObject {
                        //     [default] interface IUnknown;
                        // }
                        m_defaultInterfaceInfoList.Add(defaultInterfaceInfo);
                    }
                }
            }

            foreach (DefaultInterfaceInfo defaultInterfaceInfo in m_defaultInterfaceInfoList)
            {
                bool isExclusive = true;
                if (defaultInterfaceInfo.defaultInterface != null)
                {
                    if (interfaceToCoClassMapping.Contains(defaultInterfaceInfo.defaultInterfaceName))
                    {
                        if (interfaceToCoClassMapping[defaultInterfaceInfo.defaultInterfaceName] == null)
                        {
                            isExclusive = false;
                        }
                    }
                    defaultInterfaceInfo.isExclusive = isExclusive;
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Create the type for coclass
        /// </summary>
        public override void OnCreate()
        {
            //
            // Avoid duplicate creation
            //
            if (m_type != null)
            {
                return;
            }

            //
            // Create constructor
            // This is created before creating other methods because if anything fails, the constructor would still be valid
            // Otherwise reflection API will try to create one for us which will have incorrect setting (such as no internalcall flag)
            CreateConstructor();

            bool isConversionLoss = false;

            TypeInfo typeInfo = RefNonAliasedTypeInfo;

            if ((m_info.Settings.m_flags & TypeLibImporterFlags.PreventClassMembers) == 0)
            {
                using (TypeAttr attr = typeInfo.GetTypeAttr())
                {
                    Dictionary <Guid, bool> processedInterfaces = new Dictionary <Guid, bool>();

                    // Iterate through every interface and override the methods
                    // Process the default interface first
                    for (int m = 0; m < 2; ++m)
                    {
                        int nCount = attr.cImplTypes;
                        for (int n = 0; n < nCount; ++n)
                        {
                            TypeLibTypes.Interop.IMPLTYPEFLAGS flags = typeInfo.GetImplTypeFlags(n);
                            bool bDefault = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) != 0;
                            bool bSource  = (flags & TypeLibTypes.Interop.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0;

                            // Use exclusive or to process just the default interface on the first pass
                            if (bDefault ^ m == 1)
                            {
                                TypeInfo typeImpl = typeInfo.GetRefType(n);
                                using (TypeAttr attrImpl = typeImpl.GetTypeAttr())
                                {
                                    if (attrImpl.Guid == WellKnownGuids.IID_IUnknown || attrImpl.Guid == WellKnownGuids.IID_IDispatch)
                                    {
                                        continue;
                                    }

                                    // Skip non-dispatch interfaces that doesn't derive from IUnknown
                                    if (!attrImpl.IsDispatch && !ConvCommon.IsDerivedFromIUnknown(typeImpl))
                                    {
                                        continue;
                                    }

                                    // Skip duplicate interfaces in type library
                                    // In .IDL you can actually write:
                                    // coclass A
                                    // {
                                    //     interface IA;
                                    //     interface IA;
                                    //     ...
                                    // }
                                    //
                                    if (!processedInterfaces.ContainsKey(attrImpl.Guid))
                                    {
                                        HandleParentInterface(typeImpl, bSource, ref isConversionLoss, bDefault);
                                        processedInterfaces.Add(attrImpl.Guid, true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //
            // Emit ComConversionLoss attribute if necessary
            //
            if (isConversionLoss)
            {
                m_typeBuilder.SetCustomAttribute(CustomAttributeHelper.GetBuilderForComConversionLoss());
            }

            m_type = m_typeBuilder.CreateType();
        }