日期:2014-05-18 浏览次数:21213 次
ropertyInfo pi = (typeof(System.Windows.Forms.Button)).GetProperty("Events",
BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList ehl = (EventHandlerList)pi.GetValue(yourbutton, 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++)
{
Console.WriteLine(dels[i].Method.Name);//这里会输出所有方法名称
}
}
------解决方案--------------------
我觉得没有特别的需求情况下没必要知道所有的方法吧??
你只要在加事件之前先-=就好了
button2.Click -= new EventHandler(button2_Click);
button2.Click += new EventHandler(button2_Click);
------解决方案--------------------
Delegate[] delDirectory = button2.Click.GetInvocationList();
if (delDirectory != null && delDirectory.Length > 0)
{
foreach (EventHandle handle in delDirectory)
{
if (handle == button2_Click)
{
//已注册
}
else
{
//未注册
}
}
}
else
{
//未注册
}
------解决方案--------------------
简单点话 注册事件 先移除 然后在注册
或者可以到事件列表里面去判断是否存在该事件 例如 Zine_Alone 所说的