Example #1
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);
        }
Example #2
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));
            }
        }