批量清除为Button注册的事件
生活随笔
收集整理的這篇文章主要介紹了
批量清除为Button注册的事件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
要判斷Button.Click是否已經(jīng)綁定了Click只能去Button的EventHandlerList里面找
事實上, C#的Control封裝了EventHandlerList, 但它是protected的, 所以我們不能簡單的看到它的存在, 不過, 如果你走Debug Mode的話, 還是可以看得很清楚的, 但如果真要把它拿出來用, 就只能用Reflect了
PropertyInfo propertyInfo = (typeof(System.Windows.Forms.Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(btn1, null); FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic); Delegate d = eventHandlerList[fieldInfo.GetValue(null)]; if (d != null) {foreach (Delegate temp in d.GetInvocationList()){Console.WriteLine(temp.Method.Name);//這個地方可以清除所有的委托,也可以使用條件清除指定委托,沒有辦法直接清除所有的 } }轉(zhuǎn)載于:https://www.cnblogs.com/jRoger/articles/2502110.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的批量清除为Button注册的事件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样设计一个良好大数据处理的解决方案?
- 下一篇: navigator.geolocatio