获取 MethodBase.GetCurrentMethod() 但没有参数
Getting the MethodBase.GetCurrentMethod() but without Parameters
我有一个功能
public void AddPerson(string name)
{
Trace.WriteLine(MethodBase.GetCurrentMethod());
}
预期输出为
void AddPerson(string name)
但我希望输出的方法名没有参数。
void AddPerson()
GetCurrentMethod
方法 return 是一个 MethodBase
对象,而不是字符串。因此,如果您想要一个不同于 .ToString()
的 return 字符串,您可以从 MethodBase
属性或只是 return Name
属性,喜欢:
Trace.WriteLine(MethodBase.GetCurrentMethod().Name);
要可靠地做到这一点将是一个问题,您将不得不构建它,即 return 类型、名称、泛型类型、访问修饰符等。
例如:
static void Main(string[] args)
{
var methodBase = MethodBase.GetCurrentMethod() as MethodInfo;
Console.WriteLine($"{methodBase.ReturnType.Name} {methodBase.Name}()");
}
输出:
Void Main()
陷阱,你在追一个移动的目标:
public static (string, string) Blah(int index)
{
var methodBase = MethodBase.GetCurrentMethod() as MethodInfo;
Console.WriteLine(MethodBase.GetCurrentMethod());
Console.WriteLine($"{methodBase.ReturnType.Name} {methodBase.Name}()");
return ("sdf","dfg");
}
输出:
System.ValueTuple`2[System.String,System.String] Blah(Int32)
ValueTuple`2 Blah()
另一个选项只是用正则表达式输出参数,如下所示:(?<=\().*(?<!\))
.
我有一个功能
public void AddPerson(string name)
{
Trace.WriteLine(MethodBase.GetCurrentMethod());
}
预期输出为
void AddPerson(string name)
但我希望输出的方法名没有参数。
void AddPerson()
GetCurrentMethod
方法 return 是一个 MethodBase
对象,而不是字符串。因此,如果您想要一个不同于 .ToString()
的 return 字符串,您可以从 MethodBase
属性或只是 return Name
属性,喜欢:
Trace.WriteLine(MethodBase.GetCurrentMethod().Name);
要可靠地做到这一点将是一个问题,您将不得不构建它,即 return 类型、名称、泛型类型、访问修饰符等。
例如:
static void Main(string[] args)
{
var methodBase = MethodBase.GetCurrentMethod() as MethodInfo;
Console.WriteLine($"{methodBase.ReturnType.Name} {methodBase.Name}()");
}
输出:
Void Main()
陷阱,你在追一个移动的目标:
public static (string, string) Blah(int index)
{
var methodBase = MethodBase.GetCurrentMethod() as MethodInfo;
Console.WriteLine(MethodBase.GetCurrentMethod());
Console.WriteLine($"{methodBase.ReturnType.Name} {methodBase.Name}()");
return ("sdf","dfg");
}
输出:
System.ValueTuple`2[System.String,System.String] Blah(Int32)
ValueTuple`2 Blah()
另一个选项只是用正则表达式输出参数,如下所示:(?<=\().*(?<!\))
.