2

是的,我试图让标题变小,但我做不到。

我对反思很陌生,所以我正在努力解决一个我不知道是否有可能解决的问题。

我将使用一些简单的委托示例来描述它。

public void Main() {
    var manager = new EvManager();
    var class1 = new Class1(manager);
    var class2 = new Class2(manager);

    manager.ExecuteIt(5, 12);
    /*
    This outputs:
    In Class1 -> 17
    In Class2 -> 18
    */
}

public class EvManager {
    public delegate void SumDelegate(int a, int b);
    private SumDelegate sum;

    public void AddDelegate(SumDelegate s) {
        sum += s;
    }

    public void ExecuteIt(int a, int b) {
        sum.Invoke(a, b);
    }
}

public class Class1 {
    public Class1(EvManager m) {
        m.AddDelegate(MySum);
    }

    private void MySum(int a, int b) {
        Console.Write("In Class1 -> " + (a + b));
    }
}

public class Class2 {
    public Class2(EvManager m) {
        m.AddDelegate(MyOtherSum);
    }

    private void MyOtherSum(int a, int b){
        Console.Write("In Classe2 -> " + (a + b));
    }
}

好的,这就是例子。我想从中得到什么?我希望通过 EvManager “sum”委托属性能够访问它调用的所有方法的具体实现。

这基本上是我想要的:

class EvManager {
    private SumDelegate sum;
    public void ExecuteIt(int a, int b) {
        var invocationList = sum.GetInvocationList();
        foreach (var m in invocationList) {
            // m is a reference to the annonymous call.
            // Through reflection, I want to access the concrete method name.
            // In this case, the first iteration "MySum", and the second "MyOtherSum"
            // Is this possible?

            // Like...
            // var concreteMethodName = m.GetMethod().ConcreteCallerType.GetMethod(m.GetConreteMethodName());
            // Or something like that?
        }   
    }
}

希望我把我的问题说清楚了,这让我很生气。

4

4 回答 4

1

Method 属性保存有关真实方法的信息:

var mName = m.Method.Name;
于 2015-05-08T15:37:22.467 回答
1

您可以将以下方法添加到EvManager

public void ListDelegates()
{
    foreach (var m in sum.GetInvocationList())
    {
        Console.WriteLine(m.Method.Name);
    }
}

调用它会给你你想要的:

MySum
MyOtherSum

看看这里Delegate类参考。

于 2015-05-08T15:39:00.307 回答
1

实际上,这很简单。使用方法属性。

检查此代码:

using System;

namespace ConsoleApplication2
{

    public class EvManager
    {
        public delegate int SumDelegate(int a, int b);
        private SumDelegate sum;

        public void AddDelegate(SumDelegate s)
        {
            sum += s;
        }

        public void ExecuteIt(int a, int b)
        {
            foreach (var m in sum.GetInvocationList())
            {
                Console.WriteLine("{0}({1}, {2}) = {3}", m.Method.Name, a, b, m.DynamicInvoke(a, b));
            }

        }
    }

    public class Class1
    {
        public Class1(EvManager m)
        {
            m.AddDelegate(FakeSum);
        }

        public int FakeSum(int a, int b)
        {
            return a - b;
        }
    }

    public class Class2
    {
        public Class2(EvManager m)
        {
            m.AddDelegate(RealSum);
        }

        public int RealSum(int a, int b)
        {
            return a + b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var manager = new EvManager();
            var class1 = new Class1(manager);
            var class2 = new Class2(manager);

            manager.ExecuteIt(5, 12);
        }
    }
}

输出:

FakeSum(5, 12) = -7
RealSum(5, 12) = 17
于 2015-05-08T15:40:20.320 回答
0

您可以通过拥有财产的来获得MethodInfo每个人的分裂。delegatesMethod PropertyDelegateName

class EvManager {
    private SumDelegate sum;
    public void ExecuteIt(int a, int b) {
        var invocationList = sum.GetInvocationList();
        foreach (var m in invocationList) {
            var concreteMethodName = m.Method.Name;
        }   
    }
}
于 2015-05-08T15:35:40.693 回答