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 ...
随机推荐
- [OpenCV笔记]0.OpenCV中显示多张图像
摘要 本文主要介绍OpenCV中同时显示多张IplImage图像的方法(C++形式的多图显示需要修改,用vector<Mat>可能比较方便),有点类似MATLAB中的subplot,只是暂 ...
- hexo+github部署
废话不多少,接着上次配置的环境进行github部署. 拥有自己的github 如果还没有github的账号就注册一个吧,传送门:GitHub官网:http://www.github.com 创建一个创 ...
- Hash学习小结
Hash 简要说明 \(OI\)中一般采用进制\(hash\).模数可以用\(unsigned \ long \ long\)自然溢出,也可以使用大质数.值得一提的是,\(unsigned\ long ...
- 进阶的Redis之数据持久化RDB与AOF
大家都知道,Redis之所以性能好,读写快,是因为Redis是一个内存数据库,它的操作都几乎基于内存.但是内存型数据库有一个很大的弊端,就是当数据库进程崩溃或系统重启的时候,如果内存数据不保存的话,里 ...
- SSZipArchive使用详解
下载SSZipArchive,点击我.或者自己在这里下载. SSZipArchive功能: 解压zip文件 解压密码保护的zip文件 创建zip文件 追加到zip文件 压缩文件 使用一个名字来压缩NS ...
- Core Animation1-简介
一.Core Animation简介 * Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代 ...
- Python学习-使用Python爬取陈奕迅新歌《我们》网易云热门评论
<后来的我们>上映也有好几天了,一直没有去看,前几天还爆出退票的事件,电影的主题曲由陈奕迅所唱,特地找了主题曲<我们>的MV看了一遍,还是那个感觉.那天偶然间看到Python中 ...
- bat命令2
echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令,我们就从他们开始学起. echo 表示显示此命令后的字符 echo off 表示在此语句后所有运行的 ...
- 什么是虚拟环境、为什么使用虚拟环境、Anaconda创建、激活、退出、删除虚拟环境
一.虚拟环境 virtual environment 它是一个虚拟化,从电脑独立开辟出来的环境.通俗的来讲,虚拟环境就是借助虚拟机docker来把一部分内容独立出来,我们把这部分独立出来的东西称作“容 ...
- JavaScript中的函数(一)
javaScript中的函数实际上是对象,每一个函数都是Function类型的实例,和其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也就是一个指向函数对象的指针,也就是函数对象的一个 ...