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 ...
随机推荐
- 为Linux服务器的SSH登录启用Google两步验证
对于Linux服务器而言使用密钥登录要比使用密码登录安全的多,毕竟当前网上存在多个脚本到处进行爆破. 这类脚本都是通过扫描IP端的开放端口并使用常见的密码进行登录尝试,因此修改端口号也是非常有必要的. ...
- opengl去除控制台黑窗口
增加如下语句: #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\ ...
- HDU - 6513 Reverse It (SYSU校赛C题)(组合数学+容斥)
题目链接 题意:给定一个n*m的矩阵,可以选择至多两个子矩阵将其反转,求能形成多少种不同的矩阵. 任选一个矩阵有$C_{n+1}^{2}C_{m+1}^{2}$种方法,任选两个不同的矩阵有$C_{C_ ...
- onerror="javascript:this.src='images/defaultUpload.png';"引发的死循环错误
18:10:47.441 WARN o.s.web.servlet.PageNotFound:1101 - No mapping found for HTTP request with URI [/c ...
- 对Json的各种遍历方法
慎用for in函数(有可能由于原型链的问题导致遍历问题): 如果要是用for in 一定要使用if (obj1.hasOwnProperty(key)) {}先做判断 解决方法 :1.eval() ...
- 优化 UWP 中图片的内存占用
跟图片打交道的 UWP 应用或多或少都会遇到图片带来的性能问题,就算不主要处理图片,做个论坛做个新闻客户端都涉及到大量图片.一个帖子.一篇文章里多半都是些高清大图,这些图片一张即可占用程序 1~2M ...
- Serf 了解
Introduction to Serf Welcome to the intro guide to Serf! This guide will show you what Serf is, expl ...
- 12.Python使用requests发送post请求
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- wpf 虚拟化操作异常
根据这篇文章提供的方法会导致搜索变慢及有时候搜索不到 WPF中ItemsControl应用虚拟化时找到子元素的方法, 具体可以修改为下面代码: //Action action = () => / ...
- 优化RequireJS项目(合并与压缩)
关于RequireJS已经有很多文章介绍过了.这个工具可以将你的JavaScript代码轻易的分割成苦 干个模块(module)并且保持你的代码模块化与易维护性.这样,你将获得一些具有互相依赖关系的J ...