Search code examples
c#.netreflectionanonymous-methods

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?


Solution

  • You could capture it outside:

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

    Other than that; no.