Beispiel #1
0
 private string GetChangeManagedNameActionResult(TypeInfo typeInfo,
                                                 ConvType convType, string oldName)
 {
     if (Settings.m_ruleSet != null)
     {
         ICategory           category = TypeCategory.GetInstance();
         TypeInfoMatchTarget target   = null;
         using (TypeAttr attr = typeInfo.GetTypeAttr())
         {
             TypeLibTypes.Interop.TYPEKIND kind = attr.typekind;
             target = new TypeInfoMatchTarget(typeInfo.GetContainingTypeLib(), typeInfo, kind);
         }
         AbstractActionManager actionManager   = RuleEngine.GetActionManager();
         List <Rule>           changeNameRules = Settings.m_ruleSet.GetRule(
             category, ChangeManagedNameActionDef.GetInstance(), target);
         if (changeNameRules.Count != 0)
         {
             if (changeNameRules.Count > 1)
             {
                 Output.WriteWarning(Resource.FormatString("Wrn_RuleMultipleMatch",
                                                           ChangeManagedNameActionDef.GetInstance()),
                                     WarningCode.Wrn_RuleMultipleMatch);
             }
             Rule   changeNameRule = changeNameRules[changeNameRules.Count - 1];
             int    namespaceSplit = oldName.LastIndexOf('.');
             string oldNamespace   = "";
             if (namespaceSplit != -1)
             {
                 oldNamespace = oldName.Substring(0, namespaceSplit + 1);
             }
             return(oldNamespace + (changeNameRule.Action as ChangeManagedNameAction).NewName);
         }
     }
     return(oldName);
 }
        internal static TreeNode GetTypeLibNode(TypeLib tlb, DisplayLevel displayLevel)
        {
            string   typeLibName = tlb.GetDocumentation();
            TreeNode root        = new TreeNode(typeLibName);

            root.Tag = tlb;

            int nCount = tlb.GetTypeInfoCount();

            for (int n = 0; n < nCount; ++n)
            {
                TypeInfo type = tlb.GetTypeInfo(n);
                //string typeTypeName = type.GetDocumentation();
                //NativeType2String.AddNativeUserDefinedType(typeTypeName);

                // For dual interfaces, it has a "funky" TKIND_DISPATCH|TKIND_DUAL interface with a parter of TKIND_INTERFACE|TKIND_DUAL interface
                // The first one is pretty bad and has duplicated all the interface members of its parent, which is not we want
                // We want the second v-table interface
                // So, if we indeed has seen this kind of interface, prefer its partner
                // However, we should not blindly get the partner because those two interfaces partners with each other
                // So we need to first test to see if the interface is both dispatch & dual, and then get its partner interface
                using (TypeAttr attr = type.GetTypeAttr())
                {
                    if (attr.IsDual && attr.IsDispatch)
                    {
                        TypeInfo typeReferencedType = type.GetRefTypeNoComThrow();
                        if (typeReferencedType != null)
                        {
                            type = typeReferencedType;
                        }
                    }
                }
                TreeNode            typeInfoNode        = new TreeNode();
                TypeInfoMatchTarget typeInfoMatchTarget = null;
                root.Nodes.Add(typeInfoNode);
                using (TypeAttr attr = type.GetTypeAttr())
                {
                    TYPEKIND kind = attr.typekind;
                    typeInfoMatchTarget = new TypeInfoMatchTarget(tlb, type, kind);
                    if (displayLevel == DisplayLevel.All)
                    {
                        ProcessFunctions(type, typeInfoNode);
                        ProcessFields(type, typeInfoNode);
                    }
                }
                typeInfoNode.Text = typeInfoMatchTarget.Name + ": " +
                                    typeInfoMatchTarget.Type;
                typeInfoNode.Tag = typeInfoMatchTarget;
                SetTlbTreeNodeImage(typeInfoNode);
            }
            return(root);
        }
Beispiel #3
0
 private void DoSelect()
 {
     if (treeViewTypeLib.SelectedNode != null &&
         treeViewTypeLib.SelectedNode.Tag is TypeInfoMatchTarget)
     {
         m_typeTarget = treeViewTypeLib.SelectedNode.Tag as TypeInfoMatchTarget;
         DialogResult = DialogResult.OK;
         Dispose();
     }
     else
     {
         MessageBox.Show(Resource.FormatString("Wrn_NoTlbTypeSelected_TlbTypeSelector"));
         return;
     }
 }
        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);
        }
        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)
                    {
                        AddAttributeAction 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();
                }
            }
        }