0

Sometimes there is need to have some reflection for debugging. But the way like

System.Reflection.MethodBase.GetCurrentMethod().GetParameters().[0].Name 

returns parameter name, not the actual function name. How to find out the 'real' name for the function in this case?

void OuterFunction(Func<string, int> f)
{
   int result = f("input");
   // how to get here function name 'g' instead of 'f'?
}

int g(string s)
{
    Console.WriteLine(s);
    return 0;
}

OuterFunction(g);
Timothy Shields
  • 70,640
  • 17
  • 114
  • 164

2 Answers2

0

Write:

f.Method.Name

And don't forget to correct g to return int for it to compile ;)

JonPall
  • 794
  • 3
  • 9
0

Use the Method property of the delegate f.

void OuterFunction(Func<string, int> f)
{
    int result = f("input");
    string name = f.Method.Name;
    //name has the value "g" in your example
}

Note that if you pass in a lambda, you'll get a generated name for that lambda's body, like (Main)b__0.

Timothy Shields
  • 70,640
  • 17
  • 114
  • 164