C# 有关控件、自定义类事件中的委托链的获取、移除操作
直接来代码吧,这样干脆直接,也不耽误我午休了。一切尽在源码中。
public class ControlEventTool
{ /// <summary>
/// 移除控件的某类事件, 如Click事件
/// 2018.3.21
/// </summary>
public static void DemoRemoveControlEvents(System.Windows.Forms.Button btn, string eventName = "Click")
{
// 检索按钮的事件,这里单击事件的名字是EventClick,要注意的
Delegate[] invokeList = GetObjectEventList(btn, "Event" + eventName);
if (invokeList != null)
{
foreach (Delegate del in invokeList)
{
// 我已经测试,事件被全部取消了,没有问题。
typeof(System.Windows.Forms.Button).GetEvent(eventName).RemoveEventHandler(btn, del);
}
}
} /// <summary>
/// 获取控件指定事件的委托列表
/// </summary>
/// <param name="p_Control">对象</param>
/// <param name="p_EventName">事件名 EventClick、EventDoubleClick、EventMouseMove</param>
/// <returns>委托列</returns>
public static Delegate[] GetObjectEventList(Control p_object, string p_EventName)
{
PropertyInfo _PropertyInfo;
FieldInfo fieldInfo;
EventHandlerList evList;
Delegate d;
object _EventList; _PropertyInfo = p_object.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
if (_PropertyInfo == null) return null;
_EventList = _PropertyInfo.GetValue(p_object, null); if (_EventList == null || !(_EventList is EventHandlerList)) return null;
evList = (EventHandlerList)_EventList;
fieldInfo = (typeof(Control)).GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic);
if (fieldInfo == null) return null; d = evList[fieldInfo.GetValue(p_object)];
if (d == null) return null; return d.GetInvocationList();
} public static void PrintControlEventDelegateList(System.Windows.Forms.Button btn, string eventName = "MouseMove")
{ PropertyInfo pi = btn.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);//EventClick FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("Event" + eventName, BindingFlags.Static | BindingFlags.NonPublic);
Delegate d = ehl[fieldInfo.GetValue(null)]; if (d == null)
{
Console.WriteLine("Typed Event \"{0}\" not exist in target control!", eventName);
return;
} foreach (Delegate del in d.GetInvocationList())
Console.WriteLine(del.Method.Name);
} /// <summary>
/// 对于指定类中自定义事件,移除其中的委托链的全部订阅方法,
/// 或者移除委托链中的指定方法名的订阅。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="c"></param>
/// <param name="event_name"></param>
/// <param name="methodname"></param>
public static void RemoveEvent<T>(T c, string event_name, string methodname = "")
{
Delegate[] invokeList = GetObjectEventList_V2(c, event_name);
if (invokeList == null) return; foreach (Delegate del in invokeList)
{
if (methodname!="" && del.Method.Name != methodname)
continue;
typeof(T).GetEvent(event_name).RemoveEventHandler(c, del);
//Console.WriteLine("Remove an event of " + event_name);
}
} public static Delegate[] GetObjectEventList_V2(object p_object, string p_EventName)
{
FieldInfo _fieldInfo;
Delegate _ObjDele;
object _FieldValue; _fieldInfo = p_object.GetType().GetField(p_EventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public);
if (_fieldInfo == null) return null;
_FieldValue = _fieldInfo.GetValue(p_object); if (_FieldValue == null || !(_FieldValue is Delegate)) return null;
_ObjDele = (Delegate)_FieldValue; return _ObjDele.GetInvocationList();
} #region Demo2 public class TEST
{
public event EventHandler AA; public void Foo()
{
if (AA!=null)
{
AA(this,new EventArgs()); //invoke the event
}
}
} public static void DemoUse2()
{ TEST obj_a = new TEST();
obj_a.AA += obj_a_AA;
obj_a.Foo(); RemoveEvent<TEST>(obj_a, "AA");
obj_a.Foo(); Console.WriteLine("Finished!"); } static void obj_a_AA(object sender, EventArgs e)
{
Console.WriteLine("Evnet rasied!");
} #endregion #region Demo public static void DemoUse()
{ System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
btn.Click += new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click2);
btn.Click += new EventHandler(btn_Click3);
btn.MouseMove += btn_MouseMove;
btn.MouseMove += btn_MouseMove2; // print before
Console.WriteLine("Before");
PrintControlEventDelegateList(btn);
PrintControlEventDelegateList(btn, "Click"); // del delegate
DemoRemoveControlEvents(btn, "Click"); // print after
Console.WriteLine("After");
PrintControlEventDelegateList(btn);
PrintControlEventDelegateList(btn, "Click"); } static void btn_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
throw new NotImplementedException();
} static void btn_MouseMove2(object sender, System.Windows.Forms.MouseEventArgs e)
{
throw new NotImplementedException();
} static void btn_Click(object sender, EventArgs e)
{
Console.WriteLine("Click1");
} static void btn_Click2(object sender, EventArgs e)
{
Console.WriteLine("Click2");
} static void btn_Click3(object sender, EventArgs e)
{
Console.WriteLine("Click3");
} #endregion }
C# 有关控件、自定义类事件中的委托链的获取、移除操作的更多相关文章
- 陈年佳酿之 - Winform ListView 控件 double click 事件中获取选中的row与column
背景 最近收到了一个关于以前项目的维护请求,那时的楼主还是刚刚工作的小青年~~~ 项目之前使用的是.net/winform.今天重新打开代码,看着之前在FrameWork2.0下面的代码, 满满的回忆 ...
- 在CTreeCtrl控件点击事件中获取点击的项
网上搜了一下,有两种方法: 1.使用GetSelectedItem() HTREEITEM hItem = m_treeCtrl.GetSelectedItem(); CString strText ...
- C#中combobox 控件属性、事件、方法
一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
- 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件
[源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...
- 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件
[源码下载] 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 作者: ...
- asp.net中的ListBox控件添加双击事件
问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...
- 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件
一.控制ChartControl的Y轴范围 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件,具体代码如下: ...
- winform自定义控件中其他遮挡控件点击事件
自定义控件在其他窗口调用时,里面的lable阻挡了控件的点击事件 解决方法 自定义控件中lable的 点击事件 private void Lable1_Click(object sender, Eve ...
随机推荐
- 【sklearn】from sklearn.extermals import joblib(保存模型和加载模型)
原创博文,转载请注明出处! sklearn中保存和加载模型的方法 1.载入模块 from sklearn.externals joblib. model = joblib. # -*- coding: ...
- LeetCode Number of Atoms
原题链接在这里:https://leetcode.com/problems/number-of-atoms/description/ 题目: Given a chemical formula (giv ...
- HDU4864 Task
题意 Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, t ...
- WPF 使用MahApps.Metro UI库
在WPF中要想使用Metro风格是很简单的,可以自己画嘛.. 但是为了节省时间,哈,今天给大家推荐一款国外Metro风格的控件库. 本文只起到抛砖引玉的作用,有兴趣还是推荐大家上官网,Thanks,官 ...
- parceljs 基本使用———又一个前端构建工具
备注: 又一个新的前端构建工具 1. 安装 yarn global add parcel-bundler 2. 初始化项目 yarn init -y 3. 基本代码 a. 创建 index. ...
- vault key 管理工具
Vault is a tool for securely accessing secrets. A secret is anything that you want to tightly contro ...
- openresty websocket 使用
openresty websocket 使用 1. 代码如下: local server =require"resty.websocket.server" local wb, ...
- 微软Enterprise Library 4.1和Unity 1.2
说明 微软模式与实践团队今天发布了Enterprise Library 4.1和Unity 1.2版本,这次发布的主要新特性如下: 1. 支持Visual Studio 2008 SP1 2. Uni ...
- 开始创建一个 Vue 项目
开始创建一个 Vue 项目 安装 nodejs 略 安装 npm 默认安装时自带了 npm 安装 cnpm 为了更快的下载组件,使用cnpm,cnpm 是淘宝前端的镜像. 使用 npm 安装 cnpm ...
- gitlab安装、配置与阿里云产品集成
https://www.ilanni.com/?p=12819 一.gitlab安装与部署 gitlab的安装可以分为源码安装和通过安装包进行安装,要是按照我以前的写作习惯的话,我也会把源码安装在本文 ...