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
  • 75,459
  • 18
  • 120
  • 173
Igor Seliverstov
  • 51
  • 1
  • 1
  • 5

2 Answers2

0

Write:

f.Method.Name

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

JonPall
  • 814
  • 4
  • 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
  • 75,459
  • 18
  • 120
  • 173