现在的位置: 首页 > 综合 > 正文

C# 移除事件

2012年12月15日 ⁄ 综合 ⁄ 共 668字 ⁄ 字号 评论关闭
去除事件
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

抱歉!评论已关闭.