• 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
  • 相关阅读:
    thinkphp中ajax使用实例(thinkphp内置支持ajax)
    Uncaught SyntaxError: Invalid regular expression flags(看页面源代码)
    php函数实现显示几秒前,几分钟前,几天前等方法(网络上什么都有)
    php实现二叉树遍历
    php实现数组中的逆序对(归并排序实现:排序 辅助数组)
    基于HTML5气3D仿真培训系统
    CSDN Androidclient生产 导航帖
    MongoDB日常保养
    Machine Learning—Linear Regression
    OpenCVR 有新成员 OpenCVV OpenCVC
  • 原文地址:https://www.cnblogs.com/micro-chen/p/4097577.html
Copyright © 2020-2023  润新知