c# winform 通过编程取消事件(event)的注册

来源:互联网 发布:mac系统丢失了 编辑:程序博客网 时间:2024/06/05 22:25

     我们知道如果是委托的话我们可以让委托(delegate)对象等于null 这样的话可以取消委托时间的注册,但是如何取消事件(event)的注册呢?

    废话少说了,调用下边的方法:

 public  static void RemoveEvent(System.Windows.Forms.Control fc){            Type t = fc.GetType();            PropertyInfo pi = t.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);            EventHandlerList ehl = (EventHandlerList) pi.GetValue(fc, null);            FieldInfo fieldInfo = (typeof (System.Windows.Forms.Control)).GetField("EventClick",                                                                                   BindingFlags.Static |                                                                                   BindingFlags.NonPublic);            Delegate d = ehl[fieldInfo.GetValue(null)];            if (d != null){                foreach (Delegate temp in d.GetInvocationList()){                    ehl.RemoveHandler(fieldInfo.GetValue(null), temp);                }            }        }

原创粉丝点击