示例#1
0
        void FindInitializeComponentMethod(MTypeDef type, MMethodDef possibleInitMethod)
        {
            foreach (var methodDef in type.AllMethods)
            {
                if (methodDef.MethodDef.Name != ".ctor")
                {
                    continue;
                }
                if (methodDef.MethodDef.Body == null)
                {
                    continue;
                }
                foreach (var instr in methodDef.MethodDef.Body.Instructions)
                {
                    if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt)
                    {
                        continue;
                    }
                    if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(possibleInitMethod.MethodDef, instr.Operand as IMethod))
                    {
                        continue;
                    }

                    memberInfos.Method(possibleInitMethod).suggestedName = "InitializeComponent";
                    return;
                }
            }
        }
示例#2
0
 public void AddMethodIfEmpty(MMethodDef ifaceMethod, MMethodDef classMethod)
 {
     if (ifaceMethodToClassMethod[new MethodDefKey(ifaceMethod)] == null)
     {
         AddMethod(ifaceMethod, classMethod);
     }
 }
示例#3
0
        bool CanRenameMethod(MMethodDef methodDef)
        {
            var methodInfo = Method(methodDef);

            if (methodDef.IsStatic())
            {
                if (methodInfo.oldName == ".cctor")
                {
                    return(false);
                }
            }
            else if (methodDef.IsVirtual())
            {
                if (DotNetUtils.DerivesFromDelegate(type.TypeDef))
                {
                    switch (methodInfo.oldName)
                    {
                    case "BeginInvoke":
                    case "EndInvoke":
                    case "Invoke":
                        return(false);
                    }
                }
            }
            else
            {
                if (methodInfo.oldName == ".ctor")
                {
                    return(false);
                }
            }
            return(true);
        }
示例#4
0
 // Returns the previous classMethod, or null if none
 public MMethodDef AddMethod(ITypeDefOrRef iface, MMethodDef ifaceMethod, MMethodDef classMethod)
 {
     if (!interfaceMethods.TryGetValue(iface, out var info))
     {
         throw new ApplicationException("Could not find interface");
     }
     return(info.AddMethod(ifaceMethod, classMethod));
 }
示例#5
0
 public void AddMethodIfEmpty(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod)
 {
     if (!interfaceMethods.TryGetValue(iface.typeRef, out var info))
     {
         throw new ApplicationException("Could not find interface");
     }
     info.AddMethodIfEmpty(ifaceMethod, classMethod);
 }
示例#6
0
        string GetPinvokeName(MMethodDef methodDef)
        {
            var entryPoint = methodDef.MethodDef.ImplMap.Name.String;

            if (Regex.IsMatch(entryPoint, @"^#\d+$"))
            {
                entryPoint = DotNetUtils.GetDllName(methodDef.MethodDef.ImplMap.Module.Name.String) + "_" + entryPoint.Substring(1);
            }
            return(entryPoint);
        }
示例#7
0
        public void RenameMethod(MMethodDef methodDef, string methodName)
        {
            if (!CanRenameMethod(methodDef))
            {
                return;
            }
            var methodInfo = Method(methodDef);

            variableNameState.AddMethodName(methodName);
            methodInfo.Rename(methodName);
        }
示例#8
0
        public void renameMethod(MMethodDef methodDef, string methodName)
        {
            if (!canRenameMethod(methodDef))
            {
                return;
            }
            var methodInfo = method(methodDef);

            variableNameState.addMethodName(methodName);
            methodInfo.rename(methodName);
        }
示例#9
0
 void RenameSpecialMethod(MMethodDef methodDef, string newName)
 {
     if (methodDef == null)
     {
         return;
     }
     if (methodDef.IsVirtual())
     {
         return;
     }
     RenameMethod(methodDef, newName);
 }
示例#10
0
        // Returns the previous method, or null if none
        public MMethodDef AddMethod(MMethodDef ifaceMethod, MMethodDef classMethod)
        {
            var ifaceKey = new MethodDefKey(ifaceMethod);

            if (!ifaceMethodToClassMethod.ContainsKey(ifaceKey))
            {
                throw new ApplicationException("Could not find interface method");
            }

            ifaceMethodToClassMethod.TryGetValue(ifaceKey, out var oldMethod);
            ifaceMethodToClassMethod[ifaceKey] = classMethod;
            return(oldMethod);
        }
示例#11
0
        void RenameMethod(MMethodDef methodDef)
        {
            if (methodDef.IsVirtual())
            {
                throw new ApplicationException("Can't rename virtual methods here");
            }
            if (!CanRenameMethod(methodDef))
            {
                return;
            }

            var info = Method(methodDef);

            if (info.renamed)
            {
                return;
            }
            info.renamed = true;
            var checker = NameChecker;

            // PInvoke methods' EntryPoint is always valid. It has to, so always rename.
            bool isValidName     = NameChecker.IsValidMethodName(info.oldName);
            bool isExternPInvoke = methodDef.MethodDef.ImplMap != null && methodDef.MethodDef.RVA == 0;

            if (!isValidName || isExternPInvoke)
            {
                INameCreator nameCreator = null;
                string       newName     = info.suggestedName;
                string       newName2;
                if (methodDef.MethodDef.ImplMap != null && !string.IsNullOrEmpty(newName2 = GetPinvokeName(methodDef)))
                {
                    newName = newName2;
                }
                else if (methodDef.IsStatic())
                {
                    nameCreator = variableNameState.staticMethodNameCreator;
                }
                else
                {
                    nameCreator = variableNameState.instanceMethodNameCreator;
                }
                if (!string.IsNullOrEmpty(newName))
                {
                    nameCreator = new NameCreator2(newName);
                }
                RenameMethod(methodDef, variableNameState.GetNewMethodName(info.oldName, nameCreator));
            }
        }
示例#12
0
        static bool IsEventHandler(MMethodDef methodDef)
        {
            var sig = methodDef.MethodDef.MethodSig;

            if (sig == null || sig.Params.Count != 2)
            {
                return(false);
            }
            if (sig.RetType.ElementType != ElementType.Void)
            {
                return(false);
            }
            if (sig.Params[0].ElementType != ElementType.Object)
            {
                return(false);
            }
            if (!sig.Params[1].FullName.Contains("EventArgs"))
            {
                return(false);
            }
            return(true);
        }
示例#13
0
 public MethodInfo Method(MMethodDef method) => memberInfos.Method(method);
示例#14
0
 public MethodInfo Method(MMethodDef method)
 {
     return(memberInfos.Method(method));
 }
示例#15
0
 // Returns the previous classMethod, or null if none
 public MMethodDef AddMethod(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod) =>
 AddMethod(iface.typeRef, ifaceMethod, classMethod);
示例#16
0
 public MethodInfo Method(MMethodDef method)
 {
     return(allMethodInfos[method]);
 }
示例#17
0
 public MethodInfo(MMethodDef methodDef)
     : base(methodDef)
 {
 }
示例#18
0
 public void Add(MMethodDef m) => methods.Add(m);
示例#19
0
        void PrepareRenameMethodArgs(MMethodDef methodDef)
        {
            VariableNameState newVariableNameState = null;
            ParamInfo         info;

            if (methodDef.VisibleParameterCount > 0)
            {
                if (IsEventHandler(methodDef))
                {
                    info = Param(methodDef.ParamDefs[methodDef.VisibleParameterBaseIndex]);
                    if (!info.GotNewName())
                    {
                        info.newName = "sender";
                    }

                    info = Param(methodDef.ParamDefs[methodDef.VisibleParameterBaseIndex + 1]);
                    if (!info.GotNewName())
                    {
                        info.newName = "e";
                    }
                }
                else
                {
                    newVariableNameState = variableNameState.CloneParamsOnly();
                    var checker = NameChecker;
                    foreach (var paramDef in methodDef.ParamDefs)
                    {
                        if (paramDef.IsHiddenThisParameter)
                        {
                            continue;
                        }
                        info = Param(paramDef);
                        if (info.GotNewName())
                        {
                            continue;
                        }
                        if (!checker.IsValidMethodArgName(info.oldName))
                        {
                            info.newName = newVariableNameState.GetNewParamName(info.oldName, paramDef.ParameterDef);
                        }
                    }
                }
            }

            info = Param(methodDef.ReturnParamDef);
            if (!info.GotNewName())
            {
                if (!NameChecker.IsValidMethodReturnArgName(info.oldName))
                {
                    if (newVariableNameState == null)
                    {
                        newVariableNameState = variableNameState.CloneParamsOnly();
                    }
                    info.newName = newVariableNameState.GetNewParamName(info.oldName, methodDef.ReturnParamDef.ParameterDef);
                }
            }

            if ((methodDef.Property != null && methodDef == methodDef.Property.SetMethod) ||
                (methodDef.Event != null && (methodDef == methodDef.Event.AddMethod || methodDef == methodDef.Event.RemoveMethod)))
            {
                if (methodDef.VisibleParameterCount > 0)
                {
                    var paramDef = methodDef.ParamDefs[methodDef.ParamDefs.Count - 1];
                    Param(paramDef).newName = "value";
                }
            }
        }
示例#20
0
 public MethodInst(MMethodDef origMethodDef, IMethodDefOrRef methodRef)
 {
     this.origMethodDef = origMethodDef;
     this.methodRef     = methodRef;
 }
示例#21
0
 public MethodInfo Method(MMethodDef method) => allMethodInfos[method];