使用方法类型生成类的方法列表

我想生成一个类或类目录中的所有方法的列表。 我还需要他们的返回类型。 将它输出到文本文件将会…有没有人知道一个工具,加入VS或什么会做任务? 我顺便使用C#代码和Visual Studio 2008作为IDE

当然 – 使用Type.GetMethods()。 你需要指定不同的绑定标志来获取非公共方法等。这是一个非常粗略但可行的起点:

using System; using System.Linq; class Test { static void Main() { ShowMethods(typeof(DateTime)); } static void ShowMethods(Type type) { foreach (var method in type.GetMethods()) { var parameters = method.GetParameters(); var parameterDescriptions = string.Join (", ", method.GetParameters() .Select(x => x.ParameterType + " " + x.Name) .ToArray()); Console.WriteLine("{0} {1} ({2})", method.ReturnType, method.Name, parameterDescriptions); } } } 

输出:

 System.DateTime Add (System.TimeSpan value) System.DateTime AddDays (System.Double value) System.DateTime AddHours (System.Double value) System.DateTime AddMilliseconds (System.Double value) System.DateTime AddMinutes (System.Double value) System.DateTime AddMonths (System.Int32 months) System.DateTime AddSeconds (System.Double value) System.DateTime AddTicks (System.Int64 value) System.DateTime AddYears (System.Int32 value) System.Int32 Compare (System.DateTime t1, System.DateTime t2) System.Int32 CompareTo (System.Object value) System.Int32 CompareTo (System.DateTime value) System.Int32 DaysInMonth (System.Int32 year, System.Int32 month) 

(等等)

您可以通过reflection轻松获得这些列表。 例如,使用Type.GetMethods()

 using (StreamWriter sw = new StreamWriter("C:/methods.txt")) { foreach (MethodInfo item in typeof(MyType).GetMethods()) { sw.WriteLine(item.Name); } }