您的位置:首页 > 其它

如何取得某个菜单所绑定的所有事件处理程序

2009-06-06 07:23 549 查看
ToolStripItem item = sender as ToolStripItem;

PropertyInfo propertyInfo = (typeof(ToolStripItem)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(item, null);
FieldInfo fieldInfo = (typeof(ToolStripItem)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
Delegate d = eventHandlerList[fieldInfo.GetValue(null)];
if (d != null)
{
foreach (Delegate temp in d.GetInvocationList())
{

//这里已经取得了所有Click事件绑定的处理程序,可以做一些事情。例如下面的代码是撤销所有的事件注册
item.Click -= temp as EventHandler;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: