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; } } }
public void AddMethodIfEmpty(MMethodDef ifaceMethod, MMethodDef classMethod) { if (ifaceMethodToClassMethod[new MethodDefKey(ifaceMethod)] == null) { AddMethod(ifaceMethod, classMethod); } }
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); }
// 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)); }
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); }
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); }
public void RenameMethod(MMethodDef methodDef, string methodName) { if (!CanRenameMethod(methodDef)) { return; } var methodInfo = Method(methodDef); variableNameState.AddMethodName(methodName); methodInfo.Rename(methodName); }
public void renameMethod(MMethodDef methodDef, string methodName) { if (!canRenameMethod(methodDef)) { return; } var methodInfo = method(methodDef); variableNameState.addMethodName(methodName); methodInfo.rename(methodName); }
void RenameSpecialMethod(MMethodDef methodDef, string newName) { if (methodDef == null) { return; } if (methodDef.IsVirtual()) { return; } RenameMethod(methodDef, newName); }
// 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); }
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)); } }
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); }
public MethodInfo Method(MMethodDef method) => memberInfos.Method(method);
public MethodInfo Method(MMethodDef method) { return(memberInfos.Method(method)); }
// Returns the previous classMethod, or null if none public MMethodDef AddMethod(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod) => AddMethod(iface.typeRef, ifaceMethod, classMethod);
public MethodInfo Method(MMethodDef method) { return(allMethodInfos[method]); }
public MethodInfo(MMethodDef methodDef) : base(methodDef) { }
public void Add(MMethodDef m) => methods.Add(m);
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"; } } }
public MethodInst(MMethodDef origMethodDef, IMethodDefOrRef methodRef) { this.origMethodDef = origMethodDef; this.methodRef = methodRef; }
public MethodInfo Method(MMethodDef method) => allMethodInfos[method];