using System.Reflection;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load1;
this.Load += Form1_Load2;
// 1. 取得 Form 的非公開 Events 屬性 (EventHandlerList)
PropertyInfo pi = typeof(Form).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList ehl = (EventHandlerList)pi.GetValue(this, null);
// 2. 取得 Load 事件的 Key (EVENT_LOAD)
FieldInfo fi = typeof(Form).GetField("EVENT_LOAD", BindingFlags.Static | BindingFlags.NonPublic);
// 3. 從 EventHandlerList 取得委派
Delegate d = ehl[fi.GetValue(null)];
// 4. 列出所有訂閱方法
if (d != null)
foreach (Delegate del in d.GetInvocationList())
Console.WriteLine(del.Method.Name); // 輸出: Form1_Load1, Form1_Load2
}
private void Form1_Load1(object sender, EventArgs e) { }
private void Form1_Load2(object sender, EventArgs e) { }
}
Events 屬性儲存所有事件的委派鏈結(EventHandlerList)。Key(如 EVENT_LOAD)。GetInvocationList() 回傳鏈結中的每個委派,Method.Name 即為方法名稱。此技巧可用於執行期診斷事件訂閱狀況、防止重複訂閱等。