https://www.cnblogs.com/zszh/p/11321005.html

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

【异常类型】: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);
}
}
}
}
}
}

静态类方法

        /// <summary>
/// 移除此静态类指定事件名称的所有事件绑定
/// </summary>
/// <param name="eventname"></param>
public static void RemoveAllEvent(string eventname)
{
System.Reflection.FieldInfo _Field = typeof(GlobalVariables).GetField(eventname, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
if (_Field != null)
{
object _FieldValue = _Field.GetValue(typeof(GlobalVariables));
if (_FieldValue != null && _FieldValue is Delegate)
{
Delegate _ObjectDelegate = (Delegate)_FieldValue;
Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
if (invokeList != null)
{
foreach (var item in typeof(GlobalVariables).GetEvents())
{
if (item.Name == eventname)
{
foreach (Delegate del in invokeList)
{
item.RemoveEventHandler(typeof(GlobalVariables), del);
}
break;
}
}
}
}
} }

测试:

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);
}
}
}
}
}
}
}
}
        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();
}

c# 移除类中所有事件的绑定的更多相关文章

  1. 为什么需要在 React 类组件中为事件处理程序绑定this?

    https://juejin.im/post/5afa6e2f6fb9a07aa2137f51 事件绑定作为回调函数参数传递给函数,丢失其上下文,执行的是默认绑定,不是隐式绑定 类声明和类表达式的主体 ...

  2. javascript中onload事件如何绑定及执行顺序

    onload事件,顾名思义就是为了js程序能够在网页加载成功过后进行执行.对于放在head里面的js来说,非常必要. 如何给网页绑定onload,主要有三种方式: window.onload=func ...

  3. 1 .net中自定义事件的步骤

    1 申明一个自定义的类并且继承事件的基类 public class ClientSocketModelConnectedEvent:EventArgs { private string param; ...

  4. C#中OnLoad事件和Form1_Load事件的区别

    在学习<GDI+高级编程>第二章的过程中遇到一个疑问,就是为何有的代码用的是覆写一个OnLoad事件,而平日里我用的一般是Form1_Load事件,这两个函数很相近,但是具体有什么关系呢? ...

  5. python 类中方法总结 --- 实例方法、类方法、静态方法

    在python的类语法中,可以出现三种方法,具体如下: (1)实例方法 1)第一个参数必须是实例本身,一般使用[self]表示. 2)在实例方法中,可以通过[self]来操作实例属性,[类名]来操作类 ...

  6. Jquery 移除 html中绑定的onClick事件

    HTML绑定示例: <button class="edit" onClick="showTurnEdit(this)">编辑</button& ...

  7. QWidget类中默认是忽略inputMethodEvent事件(要获取输入的内容就必须使用这个事件)

    因为项目的需要以及主管的要求,准备将工程移植到Qt中,这样就可以比较容易的实现跨平台了.因为之前工程是在windows下开发的,第一个平台又是mobile所以除了底层框架之外其他的都是使用的windo ...

  8. WPF MVVM 架构 Step By Step(3)(把后台代码移到一个类中)

    我觉得大部分开发者应该已经知道怎么去解决这个问题.一般都是把后台代码(GLUE code)移动到一个类库.这个类库用来代表UI的属性和行为.任何代码当被移到一个类库中时都可以被编译成一个DLL,然后可 ...

  9. 错误:“ResourceDictionary”根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序。请移除 MouseLeftButtonDown 事件的事件处理程序.

    原文:错误:"ResourceDictionary"根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序.请移除 MouseLeftButtonDown 事件的事 ...

随机推荐

  1. Java内功心法,Set集合的详解

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  2. 压测应用服务对RabbitMQ消息的消费能力--实践脚本

    最近运维跟我反馈我负责的应用服务线上监控到消费RabbitMQ消息队列过慢,目前只有20左右,监控平台会有消息积压的告警. 开发修改了一版应用服务的版本,提交给我做压测验证. 之前没有做过消息中间件的 ...

  3. sharepointOnline的外接应用创建

    #CODE STARTS HERE $programFiles = [environment]::getfolderpath("programfiles") add-type -P ...

  4. FCC---Animate Multiple Elements at Variable Rates---还可以改循环时间,达到不同律动频率的效果

    In the previous challenge, you changed the animation rates for two similarly animated elements by al ...

  5. Xamarin学习(一)---- 环境准备

      前言: 摸索学习Xamarin的应用,以此博客跟进学习进度. 介绍:  Xamarin 提供了用于移动设备.平板电脑和桌面应用程序的跨平台开发解决方案.Xamarin 产品依赖于 Apple 和 ...

  6. Web服务器—Apache

    Apache配置文件:httpd.conf文件 # 指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入. ServerRoot "/www/ser ...

  7. [日常] 解决docker拉取镜像速度慢的问题

    将docker修改为国内镜像源 在/etc/docker/daemon.json文件中添加下面参数 此处使用的是中国科技大学的docker镜像源 {    "registry-mirrors ...

  8. Dockerfile语法简介

    Dockerfile是由一系列命令和参数构成的脚本,一个Dockerfile里面包含了构建整个image的完整命令.Docker通过docker build执行Dockerfile中的一系列命令自动构 ...

  9. 最新内核3.4)Linux 设备树加载I2C client adapter 的流程(内核3.4 高通)【转】

    转自:https://blog.csdn.net/lsn946803746/article/details/52515225 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转 ...

  10. assets文件夹与static文件夹的区别

    assets文件夹与static文件夹的区别原地址==>https://www.cnblogs.com/x123811/p/9230700.html 相同点===>assets文件夹和st ...