怎么判断事件是否已订阅某一方法

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);

上一篇:TP3.2 引入微信类


下一篇:HttpClient上传文件到微信素材乱码问题解决