使用反射移除指定对象的未知的事件的调用委托列表辅助类

 1 using System;
 2 using System.Reflection;
 3 using System.Windows.Forms;
 4 using System.ComponentModel;
 5 
 6 namespace BugQiang.RemoveEventHandler {
 7     /// <summary>
 8     /// 使用反射移除指定对象的未知的事件的调用委托列表辅助类
 9     /// </summary>
10     public sealed class RemoveEventHandlerHelper {
11         private RemoveEventHandlerHelper() { }
12 
13         static readonly BindingFlags _SEARCH_BINDINGFLAGS = BindingFlags.Instance |
14                                                                    BindingFlags.Static |
15                                                                    BindingFlags.Public |
16                                                                    BindingFlags.NonPublic;
17 
18         /// <summary>
19         /// 清除指定的对象targetObj的指定名称的事件eventName的调用委托列表
20         /// </summary>
21         /// <param name="targetObj">指定的对象</param>
22         /// <param name="eventName">事件名称</param>
23         public static void ClearEventHandler(object targetObj, string eventName) {
24             Type targetType = targetObj.GetType();
25             EventInfo eventInfo = GetEvent(targetType, eventName);
26             if (eventInfo == null) throw new ArgumentException("eventName");
27             Delegate[] eventInvocationList = GetEventInvocationList(targetObj, eventName);
28             foreach (Delegate handler in eventInvocationList) {
29                 eventInfo.RemoveEventHandler(targetObj, handler);
30             }
31         }
32 
33         /// <summary>
34         /// 获取指定的对象targetObj的指定名称的事件eventName的EventInvocationList
35         /// </summary>
36         /// <param name="target">指定的对象</param>
37         /// <param name="eventName">事件名称</param>
38         public static Delegate[] GetEventInvocationList(object targetObj, string eventName) {
39             FieldInfo eventFieldInfo = GetEventField(targetObj.GetType(), eventName, true);
40             if (eventFieldInfo != null) {
41                 object eventFieldValue = eventFieldInfo.GetValue(targetObj);
42                 if (eventFieldValue is Delegate) {
43                     return ((Delegate)eventFieldValue).GetInvocationList();
44                 } else {
45                     PropertyInfo propertyInfo = targetObj.GetType().GetProperty("Events", _SEARCH_BINDINGFLAGS);
46                     if (propertyInfo != null) {
47                         EventHandlerList eventHandlerList = propertyInfo.GetValue(targetObj, null) as EventHandlerList;
48                         if (eventHandlerList != null) {
49                             Delegate eventHanlder = eventHandlerList[eventFieldInfo.GetValue(targetObj)];
50                             if (eventHanlder != null)
51                                 return eventHanlder.GetInvocationList();
52                         }
53                     }
54                 }
55             }
56             return new Delegate[] { };
57         }
58 
59         /// <summary>
60         /// 获取指定类型target的指定名称的事件eventName的FieldInfo
61         /// </summary>
62         /// <param name="target">指定的类型</param>
63         /// <param name="eventName">事件名称</param>
64         /// <param name="inherited">是否递归搜索继承类型</param>
65         /// <returns></returns>
66         public static FieldInfo GetEventField(Type target, string eventName, bool inherited) {
67             FieldInfo eventInfo = target.GetField(eventName, _SEARCH_BINDINGFLAGS);
68             if (inherited && eventInfo == null) {
69                 if (target.BaseType != null) {
70                     if (target.BaseType == typeof(Control))
71                         eventInfo = GetEventField(target.BaseType, "Event" + eventName, false);
72                     else
73                         eventInfo = GetEventField(target.BaseType, eventName, true);
74                 }
75             }
76             return eventInfo;
77         }
78 
79         /// <summary>
80         /// 获取指定类型target的指定名称的事件eventName的EventInfo
81         /// </summary>
82         /// <param name="target">指定的类型</param>
83         /// <param name="eventName">事件名称</param>
84         /// <returns></returns>
85         public static EventInfo GetEvent(Type target, string eventName) {
86             return target.GetEvent(eventName, _SEARCH_BINDINGFLAGS);
87         }
88     }
89 }

PS:该Demo存在很大的缺憾,仅供参考,还请高人指点一二以协助小弟进行改善以及完善。

原文地址:https://www.cnblogs.com/BugQiang/p/3175186.html