你可以n檢索事件的調用列表,並且可以從每個調用中檢索目標(對於靜態eventhandler方法可以爲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)
什麼不同部位和不同的時間呢?你能提供一個你正在努力完成的具體事例嗎? –