C# 注销掉事件,解决多播委托链表的问题

c#的事件是多播委托。当绑定多个事件时,事件会依次触发,清除掉注册的委托链表:方法1
 C# Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
private void ClearAllEvents (object objectHasEvents, string eventName)
{
    if (objectHasEvents == null)
    {
        return;
    }

try
    {
        EventInfo[] events = objectHasEvents.GetType().GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        if (events == null || events.Length < 1)
        {
            return;
        }

for (int i = 0; i < events.Length; i++)
        {
            EventInfo ei = events[i];

if (ei.Name == eventName)
            {
                FieldInfo fi = ei.DeclaringType.GetField ("printPageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (fi != null)
                {
                    fi.SetValue (objectHasEvents, null);
                }

break;
            }
        }
    }
    catch
    {
    }
}

 
 
 C# Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
        //事件
 
        #region 开始查询用户业务编码事件
 /// <summary>
        /// 开始查询用户业务编码事件
        /// </summary>
        public static event EventHandler _BeginSearchUserCodeEvent;
        public static event EventHandler BeginSearchUserCodeEvent
        {
            add
            {
                ClearBeginSearchUserCodeEvent();
                _BeginSearchUserCodeEvent += value;
                last_BeginSearchUserCodeEvent.Add(value);
            }
            remove
            {
                _BeginSearchUserCodeEvent -= value;
            }
 
        }
        //上一次的BeginSearchUserCodeEvent委托
        private static List<EventHandler> last_BeginSearchUserCodeEvent = new List<EventHandler>();
        private static void ClearBeginSearchUserCodeEvent()
        {
            if (last_BeginSearchUserCodeEvent.Count <= 0)
            {
                return;
            }
            //注销掉  事件
            for (int i = 0; i < last_BeginSearchUserCodeEvent.Count; i++)
            {
                _BeginSearchUserCodeEvent -= last_BeginSearchUserCodeEvent[i];
            }
        }
        #endregion
 
 
 
        #region  结束查询用户业务编码事件
      
        /// <summary>
        /// 结束查询用户业务编码事件
        /// </summary>
        public static event EventHandler _EndSearchUserCodeEvent;
        public static event EventHandler EndSearchUserCodeEvent
        {
 
            add
            {
                ClearEndSearchUserCodeEvent();
                _EndSearchUserCodeEvent += value;
                last_EndSearchUserCodeEvent.Add(value);
            }
            remove
            {
                _EndSearchUserCodeEvent -= value;
            }
 
        }
        //上一次的EndSearchUserCodeEvent委托
        private static List<EventHandler> last_EndSearchUserCodeEvent = new List<EventHandler>();
        private static void ClearEndSearchUserCodeEvent()
        {
            if (last_EndSearchUserCodeEvent.Count <= 0)
            {
                return;
            }
            //注销掉  事件
            for (int i = 0; i < last_EndSearchUserCodeEvent.Count; i++)
            {
                _EndSearchUserCodeEvent -= last_EndSearchUserCodeEvent[i];
            }
        }
 
        #endregion
上一篇:iOS富文本的使用


下一篇:委托+内置委托方法+多播委托+lambda表达式+事件