private bool RuleEngineResolveRedirection(RuleSet ruleSet, TypeInfo typeInfo, out Type convertedType) { convertedType = null; if (ruleSet != null) { ICategory category = TypeCategory.GetInstance(); TypeLibTypes.Interop.TYPEKIND typeKind; using (TypeAttr attr = typeInfo.GetTypeAttr()) { typeKind = attr.typekind; } TypeInfoMatchTarget target = new TypeInfoMatchTarget(typeInfo.GetContainingTypeLib(), typeInfo, typeKind); AbstractActionManager actionManager = RuleEngine.GetActionManager(); List<Rule> resolveToRules = ruleSet.GetRule( category, ResolveToActionDef.GetInstance(), target); if (resolveToRules.Count != 0) { if (resolveToRules.Count > 1) { Output.WriteWarning(Resource.FormatString("Wrn_RuleMultipleMatch", ResolveToActionDef.GetInstance().GetActionName()), WarningCode.Wrn_RuleMultipleMatch); } Rule resolveToRule = resolveToRules[resolveToRules.Count - 1]; ResolveToAction action = resolveToRule.Action as ResolveToAction; try { Assembly assembly = Assembly.ReflectionOnlyLoad(action.AssemblyName); convertedType = assembly.GetType(action.ManagedTypeFullName); return true; } catch (Exception) { Output.WriteWarning(Resource.FormatString("Wrn_CannotLoadResolveToType", action.ManagedTypeFullName, action.AssemblyName), WarningCode.Wrn_CannotLoadResolveToType); } } } return false; }
/// <summary> /// Check whether the interface, which the type "extendedType" wants to implement, is a class interface /// exported by TlbExp. /// We do not support this scenario, and an exception will be thrown. /// </summary> internal static void ThrowIfImplementingExportedClassInterface( TypeInfo extendedType, IConvInterface parentInterface) { TypeInfo parentType = parentInterface.RefTypeInfo; TypeLib parentTypeLib = parentType.GetContainingTypeLib(); TypeLib thisTypeLib = extendedType.GetContainingTypeLib(); var asmName = parentTypeLib.GetCustData(CustomAttributeGuids.GUID_ExportedFromComPlus) as string; if (asmName != null) { var parentName = parentType.GetCustData(CustomAttributeGuids.GUID_ManagedName) as string; Type parentManagedType = parentInterface.RealManagedType; if (parentName != null && parentManagedType != null && parentManagedType.IsClass) { string msg = Resource.FormatString("Err_ImplementExportedClassInterface", new object[] { extendedType.GetDocumentation(), thisTypeLib.GetDocumentation(), parentType.GetDocumentation(), parentTypeLib.GetDocumentation() }); throw new TlbImpGeneralException(msg, ErrorCode.Err_ImplementExportedClassInterface); } } }
/// <summary> /// Is this type a StdOle2.Guid? The test is done using the GUID of type library /// </summary> /// <param name="type">The type</param> /// <returns>True if this type is a StdOle2.Guid</returns> private bool _IsStdOleGuid(TypeInfo type) { TypeLib typeLib = type.GetContainingTypeLib(); using (TypeLibAttr typeLibAttr = typeLib.GetLibAttr()) { if (type.GetDocumentation() == "GUID" && typeLibAttr.guid == WellKnownGuids.TYPELIBID_STDOLE2) return true; } return false; }
protected void DefineType(ConverterInfo info, TypeInfo typeInfo, bool dealWithAlias) { m_info = info; m_typeInfo = typeInfo; if (dealWithAlias) m_nonAliasedTypeInfo = ConvCommon.GetAlias(typeInfo); else m_nonAliasedTypeInfo = typeInfo; try { OnDefineType(); // // Emit SuppressUnmanagedCodeSecurityAttribute for /unsafe switch // if ((m_info.Settings.m_flags & TypeLibImporterFlags.UnsafeInterfaces) != 0) { if (ConvType != ConvType.ClassInterface && ConvType != ConvType.EventInterface) m_typeBuilder.SetCustomAttribute(CustomAttributeHelper.GetBuilderForSuppressUnmanagedCodeSecurity()); } // Rule Engine AddAttributeAction if (m_info.Settings.m_ruleSet != null) { ICategory category = TypeCategory.GetInstance(); TypeInfoMatchTarget target = null; using (TypeAttr attr = m_typeInfo.GetTypeAttr()) { TypeLibTypes.Interop.TYPEKIND kind = attr.typekind; target = new TypeInfoMatchTarget(m_typeInfo.GetContainingTypeLib(), m_typeInfo, kind); } AbstractActionManager actionManager = RuleEngine.GetActionManager(); List<Rule> addAttributeRules = m_info.Settings.m_ruleSet.GetRule( category, AddAttributeActionDef.GetInstance(), target); foreach (Rule rule in addAttributeRules) { var addAttributeAction = rule.Action as AddAttributeAction; ConstructorInfo attributeCtor; byte[] blob; bool success = true; if (addAttributeAction.GetCustomAttribute(out attributeCtor, out blob)) { try { m_typeBuilder.SetCustomAttribute(attributeCtor, blob); } catch (Exception) { success = false; } } else { success = false; } if (!success) { string name = m_typeInfo.GetDocumentation(); string msg = Resource.FormatString("Wrn_AddCustomAttributeFailed", addAttributeAction.TypeName, name); m_info.ReportEvent(WarningCode.Wrn_AddCustomAttributeFailed, msg); } } } } catch (ReflectionTypeLoadException) { throw; // Fatal failure. Throw } catch (TlbImpResolveRefFailWrapperException) { throw; // Fatal failure. Throw } catch (TlbImpGeneralException) { throw; // Fatal failure. Throw } catch (Exception) { string name = String.Empty; if (m_typeInfo != null) { try { name = m_typeInfo.GetDocumentation(); } catch (Exception) { } } if (name != String.Empty) { string msg = Resource.FormatString("Wrn_InvalidTypeInfo", name); m_info.ReportEvent(WarningCode.Wrn_InvalidTypeInfo, msg); } else { string msg = Resource.FormatString("Wrn_InvalidTypeInfo_Unnamed"); m_info.ReportEvent(WarningCode.Wrn_InvalidTypeInfo_Unnamed, msg); } // When failure, try to create the type anyway if (m_typeBuilder != null) { m_type = m_typeBuilder.CreateType(); } } }