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

     我们知道如果是委托的话我们可以让委托(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);
                }
            }
        }

你可能感兴趣的:(c#用法技巧,winform,编程,c#,null,events)