public static string Print(this MethodReference method, MethodPrintOption options) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0} {1} {2}", method.DeclaringType.FullName, method.ReturnType.Name, method.Name); PrintParameters(method.Parameters, sb, options); return(sb.ToString()); }
public static string Print(this MethodDefinition method, MethodPrintOption options) { StringBuilder sb = new StringBuilder(); bool bPrintAlias = ((MethodPrintOption.ShortNames & MethodPrintOption.ShortNames) == MethodPrintOption.ShortNames); if ((MethodPrintOption.Visiblity & options) == MethodPrintOption.Visiblity) { AddVisibility(sb, method.IsPublic, method.IsPrivate, method.IsFamily, method.IsAssembly, method.IsFamilyOrAssembly); } if ((MethodPrintOption.Modifier & options) == MethodPrintOption.Modifier) { if (method.IsVirtual && method.HasBody) { sb.AppendFormat("{0} ", "virtual"); } if (method.IsStatic) { sb.AppendFormat("{0} ", "static"); } } if ((MethodPrintOption.ReturnType & options) == MethodPrintOption.ReturnType) { string retType = null; if (bPrintAlias) { GenericInstanceType generic = method.ReturnType as GenericInstanceType; if (generic != null) { retType = generic.Print(); } } if (retType == null) { retType = TypeMapper.FullToShort(method.ReturnType.FullName); } sb.AppendFormat("{0} ", retType); } sb.Append(method.Name); if ((options & MethodPrintOption.Parameters) == MethodPrintOption.Parameters) { PrintParameters(method.Parameters, sb, options); } return(sb.ToString()); }
static void PrintParameters(Collection <ParameterDefinition> parameters, StringBuilder sb, MethodPrintOption options) { bool bPrintNames = ((options & MethodPrintOption.ParamNames) == MethodPrintOption.ParamNames); sb.Append("("); for (int i = 0; i < parameters.Count; i++) { ParameterDefinition parameter = parameters[i]; if (parameter.IsOut) { sb.Append("out "); } string paramType = null; GenericInstanceType generic = parameter.ParameterType as GenericInstanceType; if (generic != null) { paramType = generic.Print(); } if (paramType == null) { paramType = parameter.ParameterType.Name; } // Ref types seem not to be correctly parsed by mono cecil so we leave the & syntax // inside it for the time beeing. if (parameter.IsOut) { paramType = paramType.TrimEnd(new char[] { '&' }); } sb.Append(paramType); if (bPrintNames) { sb.AppendFormat(" {0}", parameter.Name); } // parameter.Name if (i != parameters.Count - 1) { sb.Append(","); } } sb.Append(")"); }