void ITypeInfo.GetImplTypeFlags(int index, out IMPLTYPEFLAGS pImplTypeFlags) { Before($"{nameof(index)}: {index}"); _wrapper.GetImplTypeFlags(index, out var t); After($"{nameof(pImplTypeFlags)}: {t}"); pImplTypeFlags = t; }
public ComInformation(TYPEATTR typeAttributes, IMPLTYPEFLAGS implTypeFlags, ITypeInfo typeInfo, string typeName, QualifiedModuleName typeModuleName, Declaration moduleDeclaration, DeclarationType typeDeclarationType) { TypeAttributes = typeAttributes; ImplTypeFlags = implTypeFlags; TypeInfo = typeInfo; TypeName = typeName; TypeQualifiedModuleName = typeModuleName; ModuleDeclaration = moduleDeclaration; TypeDeclarationType = typeDeclarationType; }
public OWCoClassInterface(ITlibNode parent, ITypeInfo ti, IMPLTYPEFLAGS impltypeflags) { Parent = parent; Listeners = parent.Listeners; _ti = ti; _flags = impltypeflags; _name = _ti.GetName(); _data = new IDLData(this); }
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) { IMPLTYPEFLAGS flags = typeInfo.GetImplTypeFlags(n); bool isDefault = (flags & IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) != 0; bool isSource = (flags & 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); // 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)) { 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); } }
/// <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) { IMPLTYPEFLAGS flags = typeInfo.GetImplTypeFlags(n); bool bDefault = (flags & IMPLTYPEFLAGS.IMPLTYPEFLAG_FDEFAULT) != 0; bool bSource = (flags & 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(); }
public void GetImplTypeFlags(int index, out IMPLTYPEFLAGS pImplTypeFlags) { throw new Exception("The method or operation is not implemented."); }
/// <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 == TYPEKIND.TKIND_COCLASS) { DefaultInterfaceInfo defaultInterfaceInfo = new DefaultInterfaceInfo(); defaultInterfaceInfo.coclass = type; defaultInterfaceInfo.coclassName = type.GetDocumentation(); for (int m = 0; m < attr.cImplTypes; ++m) { IMPLTYPEFLAGS flags = type.GetImplTypeFlags(m); bool bDefault = (flags & 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 & 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; } } }