首页 新闻 会员 周边

menuItem 事件列表

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-02-05 12:01
 1 PropertyInfo propertyInfo = (typeof(System.Windows.Forms.Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
2 EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(button1, null);
3 FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
4 Delegate d = eventHandlerList[fieldInfo.GetValue(null)];
5 if (d != null)
6 {
7 foreach (Delegate temp in d.GetInvocationList())
8 {
9 //在这里可以对事件列表进行处理
10 btnTest.Click -= (EventHandler)temp;
11 btnTest.Click += (EventHandler)temp;
12 }
13 }

上例就是将button1的click事件通过反射的方式加载到btnTest上!

但是如果要将已经知道的ContextMenu下的某项menuItem的click事件也用同样的方式加载到另一个按钮!经测试是不行的!如下

1   PropertyInfo propertyInfo0 = (typeof(System.Windows.Forms.MenuItem)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
2 EventHandlerList eventHandlerList1 = (EventHandlerList)propertyInfo0.GetValue(cm.MenuItems[0], null);

经对比发现eventHandlerList与eventHandlerList1

eventHandlerList下的head是有值的{System.ComponentModel.EventHandlerList.ListEntry}

而eventHandlerList1是为null的

想请问一下子,有什么办法取得到menuItem下面的事件列表


 

Wid纬度的主页 Wid纬度 | 初学一级 | 园豆:60
提问于:2011-12-15 14:42
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册