是的,我试图让标题变小,但我做不到。
我对反思很陌生,所以我正在努力解决一个我不知道是否有可能解决的问题。
我将使用一些简单的委托示例来描述它。
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?
}
}
}
希望我把我的问题说清楚了,这让我很生气。