【问题标题】:How to get delegate name inside delegated method?如何在委托方法中获取委托名称?
【发布时间】:2014-09-10 05:09:02
【问题描述】:

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

这是我的测试程序:

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(),但没用。

【问题讨论】:

  • action 是委托而不是方法,real 是委托方法。
  • 来自void DoAction(Action actionParam),应该给action还是actionParam
  • @Hassan Nisar,感谢指正,我会更新说明。
  • @Henk Holterman,对不起,我不明白你的意思,你能补充一些细节吗?

标签: c# delegates


【解决方案1】:

考虑

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

    //pr.action();
    tempName2();
}

你想取什么名字? tempName1、tempName2、pr.action 还是只是行动?

根据这些选择,您无法获得明确的变量名称。

【讨论】:

  • 谢谢!在这种情况下我可以得到 tempName2 吗?
  • 没有。
猜你喜欢
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
相关资源
最近更新 更多