[英]How to get delegate name inside delegated method?
如何在委托方法中獲取委托名稱?
這是我的測試程序:
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();
}
}
}
那么,如何獲得委托action
的名稱而不是方法的read
?
我已經嘗試過MethodInfo.GetCurrentMethod()
,但是沒有用。
考慮
static void Main(string[] args)
{
Program pr = new Program();
Action tempName1 = pr.action;
Action tempName2 = tempName1;
//pr.action();
tempName2();
}
您想獲得哪個名字? tempName1,tempName2,pr.action還是just action?
從這些選擇中可以得出,您無法獲得明確的變量名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.