去除事件
public static class EventExtension
{
public static void RemoveEvents<T>(this Control target,string Event)
{
FieldInfo f1 = typeof(Control).GetField(Event,BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(target.CastTo<T>());
PropertyInfo pi =target.CastTo<T>().GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(target.CastTo<T>(), null);
list.RemoveHandler(obj, list[obj]);
}
}
使用方法:
Button button=new Button();
button.RemoveEvents<Button>("EventClick");
URL:http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control