c# 移除类中所有事件的绑定

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 移除类中所有事件的绑定相关的知识,希望对你有一定的参考价值。

原文:c# 移除类中所有事件的绑定

单例中为防止多处注册事件引起异步触发时发生报错,网上找了一圈没找到想要的方法。

【异常类型】:ArgumentException
【异常信息】:该委托必须有一个目标(且仅有一个目标)。

结合网上资料整合了个方法

        /// <summary>
        /// 移除所有注册事件
        /// </summary>
        public void RemoveAllEvent()
        
            var newType = this.GetType();
            foreach (var item in newType.GetEvents())
            
                FieldInfo _Field = newType.GetField(item.Name, BindingFlags.Instance | BindingFlags.NonPublic);
                if (_Field != null)
                
                    object _FieldValue = _Field.GetValue(this);
                    if (_FieldValue != null && _FieldValue is Delegate)
                    
                        Delegate _ObjectDelegate = (Delegate)_FieldValue;
                        Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
                        if (invokeList != null)
                        
                            foreach (Delegate del in invokeList)
                            
                                item.RemoveEventHandler(this, del);
                            
                        
                    
                
            
        

 

测试:

技术图片
using System;
using System.Reflection;

namespace FormTest

    class Class1
    
        public event Action OnTest;
        public event Action OnShow;

        /// <summary>
        /// 移除所有注册事件
        /// </summary>
        public void RemoveAllEvent()
        
            var newType = this.GetType();
            foreach (var item in newType.GetEvents())
            
                FieldInfo _Field = newType.GetField(item.Name, BindingFlags.Instance | BindingFlags.NonPublic);
                if (_Field != null)
                
                    object _FieldValue = _Field.GetValue(this);
                    if (_FieldValue != null && _FieldValue is Delegate)
                    
                        Delegate _ObjectDelegate = (Delegate)_FieldValue;
                        Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
                        if (invokeList != null)
                        
                            foreach (Delegate del in invokeList)
                            
                                item.RemoveEventHandler(this, del);
                            
                        
                    
                
            
        
    
View Code
技术图片
        private void button7_Click(object sender, EventArgs e)
        
            Class1 cla = new Class1();
            cla.OnShow += cla_OnShow;
            cla.OnShow += cla_OnShow;
            cla.OnTest += cla_OnTest;

            cla.RemoveAllEvent();
        

        void cla_OnTest()
        
            throw new NotImplementedException();
        

        void cla_OnShow()
        
            throw new NotImplementedException();
        
View Code

技术图片技术图片

 

以上是关于c# 移除类中所有事件的绑定的主要内容,如果未能解决你的问题,请参考以下文章

移除类添加的事件监听器

c#通过反射移除所有事件

jquery移除绑定触发元素事件

移除类/添加类

派生类中事件绑定的事件处理函数是类实例方法使其能访问父类和派生类所有数据

jquery 鼠标移除 清除所有的事件