我有一个巨大的解决方案,我想将所有类中所有方法的列表导出到文本文件,包括它们的参数。
例如:
public static class MyMath
{
public static bool InRange(double x1, double y1, double x2, double y2, byte Range)
{
return (System.Math.Max(System.Math.Abs(x1 - x2), System.Math.Abs(y1 - y2)) <= Range);
}
public static int PointDistance(double x1, double y1, double x2, double y2)
{
return (int)System.Math.Sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
}
public static int Generate(int Min, int Max)
{
return new Random().Next(Min, Max);
}
}
我想将其导出为这样的文本文件:
bool MyMath.InRange(x1, y1, x2, y2, Range)
或
bool MyMath.InRange(double, double, double, double, byte)
我只需要一种方法来获得方法和方法。反射通过反射打印出来。
答案 0 :(得分:1)
assembly.GetTypes();
type.GetMethods();
methodInfo.Name
methodInfo.GetParameters()
methodInfo.ReturnType
例子代码:
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
var methodes = type.GetMethods();
foreach (var methodInfo in methodes)
{
var myMethodName = methodInfo.Name;
var parameters = methodInfo.GetParameters();
var returnType = methodInfo.ReturnType;
// Write to your text file
}
}
}
答案 1 :(得分:0)
以下是一个例子:
StringBuilder sb = new StringBuilder();
var type = typeof (MyMath);
sb.AppendFormat("Class Name: {0}", type.Name);
sb.AppendLine();
sb.Append("Methods: ");
sb.AppendLine();
foreach (var method in type.GetMethods())
{
sb.AppendFormat("{0} {1}.{2}( ", method.ReturnType.Name, type.Name, method.Name);
foreach (var param in method.GetParameters())
{
sb.AppendFormat("{0} {1},", param.GetType().Name, param.Name);
}
sb.Remove(sb.ToString().Length - 1, 1);
sb.Append(")");
sb.AppendLine();
}
var content = sb.ToString();
File.WriteAllText("deneme.txt",content);
答案 2 :(得分:0)
您可以使用以下代码:
Assembly assembly = Assembly.LoadFrom(@"E:\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
StringBuilder s = new StringBuilder();
foreach (Type t in assembly.GetTypes())
{
foreach (MethodInfo m in t.GetMethods())
s.Append("\n" + m.ToString() + " ");
}
然后将s
写入您的文本文件。