怎么判断事件是否已订阅某一方法
作者:互联网
1用反射
PropertyInfo pi = (typeof(Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); EventHandlerList ehl = (EventHandlerList)pi.GetValue(btnHardInfo, null);//这是你的Button FieldInfo fi = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic); Delegate d = ehl[fi.GetValue(null)]; if (d != null) { System.Delegate[] dels = d.GetInvocationList(); for (int i = 0; i < dels.Length; i++) { MsgBox.Items.Add(dels[i].Method.Name);//这里会输出所有方法名称 } }
2只要在加事件之前先-=就可以了
button2.Click -= new EventHandler(button2_Click);
button2.Click += new EventHandler(button2_Click);
标签:订阅,判断,dels,GetValue,button2,某一,BindingFlags,null,Click 来源: https://www.cnblogs.com/sinceret/p/11393565.html