• 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
  • 相关阅读:
    redis---01
    mysql优化-----索引覆盖
    mysql优化-------Myisam与innodb引擎,索引文件的区别
    mysql优化-----多列索引的左前缀规则
    mysql---列的选取原则
    boogo08---中间件
    goroutine pool,WaitGroup,chan 示例
    Android开发 |常见的内存泄漏问题及解决办法
    Android中FragmentPagerAdapter对Fragment的缓存(二)
    Android中FragmentPagerAdapter对Fragment的缓存(一)
  • 原文地址:https://www.cnblogs.com/micro-chen/p/4097577.html
Copyright © 2020-2023  润新知