Reputation: 785
How to get delegate name inside delegated method?
Here is my program for testing:
namespace Test
{
class Program
{
public Action action;
void real()
{
// I hoped it would output "action" here, but it was "real"
Console.WriteLine(MethodInfo.GetCurrentMethod().Name);
}
public Program()
{
action = real;
}
static void Main(string[] args)
{
Program pr = new Program();
pr.action();
}
}
}
So how can I get the name of delegate action
instead of method read
?
I've tried MethodInfo.GetCurrentMethod()
, but it didn't work.
Upvotes: 1
Views: 195
Reputation: 273244
Consider
static void Main(string[] args)
{
Program pr = new Program();
Action tempName1 = pr.action;
Action tempName2 = tempName1;
//pr.action();
tempName2();
}
Which name would you like to get? tempName1, tempName2, pr.action or just action?
From these choices it follows that you can't get an unambiguous variable name.
Upvotes: 1