在匿名方法使用MethodInfo.GetCurrentMethod()方法、MethodInfo、GetCurrentMethod

2023-09-03 05:01:12 作者:爱意落于星河

public static void Main(string[] args)
{
    Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name);
    a();
}

这code会返回一个不起眼的字符串,像这样:<主> b__0

This code will return an obscure string like so: <Main>b__0.

有没有忽略了匿名方法,并得到一个更可读的方法名称的方式?

Is there a way of ignoring the anonymous methods and get a more readable method name?

推荐答案

您可以外捕捉到它:

var name = MethodInfo.GetCurrentMethod().Name + ":subname";
Action a = () => Console.WriteLine(name);

除此之外,没有。

Other than that; no.