winform 獲取按鈕的單擊事件

今天開發遇到了一個需求  已經獲取了當前的btn對象,須要經過btn對象調用它的點擊事件 故百度了下解決方案 在此記錄下,以便分享。spa

var  btn=new Button;         
   PropertyInfo pi=(typeof(Button)).GetProperty("Events",BindingFlags.Instance|BindingFlags.NonPublic);
                EventHandlerList ehl=(EventHandlerList)pi.GetValue(btn,null);
                FieldInfo fi=(typeof(Control)).GetField("EventClick",BindingFlags.Static|BindingFlags.NonPublic);
                Delegate d=ehl[fi.GetValue(null)];
相關文章
相關標籤/搜索