Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MethodInfo.GetCurrentMethod() in anonymous methods

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

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?

like image 940
HuBeZa Avatar asked Jan 16 '11 10:01

HuBeZa


1 Answers

You could capture it outside:

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

Other than that; no.

like image 71
Marc Gravell Avatar answered Sep 28 '22 08:09

Marc Gravell