C# 如何在委托方法中获取委托名称?

C# 如何在委托方法中获取委托名称?,c#,delegates,C#,Delegates,如何在委托方法中获取委托名称 以下是我的测试程序: namespace Test { class Program { public Action action; void real() { // I hoped it would output "action" here, but it was "real" Console.WriteLine(MethodInfo.GetCurren

如何在委托方法中获取委托名称

以下是我的测试程序:

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();
        }
    }
}
那么,如何获取代理
操作
的名称而不是方法
读取

我尝试了
MethodInfo.GetCurrentMethod()
,但没有成功。

考虑一下

static void Main(string[] args)
{
    Program pr = new Program();
    Action tempName1 = pr.action;
    Action tempName2 = tempName1;

    //pr.action();
    tempName2();
}
你想取哪个名字?临时名称1、临时名称2、公关行动还是仅仅行动


从这些选择中,您无法获得明确的变量名

action
不是委托方法,
real
是委托方法。从
void-DoAction(action-actionParam)
,它应该给
action
还是
actionParam
?@Hassan Nisar,谢谢更正,我会更新描述。@Henk Holterman,对不起,我不明白你的意思,你能补充一些细节吗?谢谢!在这种情况下我能得到tempName2吗?不行。