您可以检索事件的调用列表,并且可以从每次调用中检索 Target(对于静态事件处理程序方法可以为 null)和 MethodInfo。
像这样的东西:
public class TestEventInvocationList {
public static void ShowEventInvocationList() {
var testEventInvocationList = new TestEventInvocationList();
testEventInvocationList.MyEvent += testEventInvocationList.MyInstanceEventHandler;
testEventInvocationList.MyEvent += MyNamedEventHandler;
testEventInvocationList.MyEvent += (s, e) => {
// Lambda expression method
};
testEventInvocationList.DisplayEventInvocationList();
Console.ReadLine();
}
public static void MyNamedEventHandler(object sender, EventArgs e) {
// Static eventhandler
}
public event EventHandler MyEvent;
public void DisplayEventInvocationList() {
if (MyEvent != null) {
foreach (Delegate d in MyEvent.GetInvocationList()) {
Console.WriteLine("Object: {0}, Method: {1}", (d.Target ?? "null").ToString(), d.Method);
}
}
}
public void MyInstanceEventHandler(object sendef, EventArgs e) {
// Instance event handler
}
}
这将产生:
Object: ConsoleApplication2.TestEventInvocationList, Method: Void MyInstanceEven
tHandler(System.Object, System.EventArgs)
Object: null, Method: Void MyNamedEventHandler(System.Object, System.EventArgs)
Object: null, Method: Void <MyMain>b__0(System.Object, System.EventArgs)