110,536
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_Click(object sender, EventArgs e)
{
// 为按钮添加两个事件
this.button13.Click += new EventHandler(button12_Click_1);
this.button13.Click += new EventHandler(button12_Click_2);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_Click_1(object sender, EventArgs e)
{
MessageBox.Show("1");
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_Click_2(object sender, EventArgs e)
{
MessageBox.Show("2");
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button14_Click(object sender, EventArgs e)
{
// 检索按钮的事件,这里单击事件的名字是EventClick,要注意的
Delegate[] invokeList = GetObjectEventList(this.button13, "EventClick");
if (invokeList != null)
{
foreach (Delegate del in invokeList)
{
// 我已经测试,事件被全部取消了,没有问题。
typeof(Button).GetEvent("Click").RemoveEventHandler(this.button13, del);
}
}
}
/// <summary>
/// 获取控件事件
/// </summary>
/// <param name="p_Control">对象</param>
/// <param name="p_EventName">事件名 EventClick EventDoubleClick </param>
/// <returns>委托列</returns>
public Delegate[] GetObjectEventList(Control p_Control, string p_EventName)
{
PropertyInfo _PropertyInfo = p_Control.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
if (_PropertyInfo != null)
{
object _EventList = _PropertyInfo.GetValue(p_Control, null);
if (_EventList != null && _EventList is EventHandlerList)
{
EventHandlerList _List = (EventHandlerList)_EventList;
FieldInfo _FieldInfo = (typeof(Control)).GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic);
if (_FieldInfo == null) return null;
Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Control)];
if (_ObjectDelegate == null) return null;
return _ObjectDelegate.GetInvocationList();
}
}
return null;
}
public class Test
{
public event EventHandler AA;
public void Foo()
{
if (AA != null) AA(this, new EventArgs());
}
}
static void Main(string[] args)
{
Test obj = new Test();
obj.AA += delegate { Console.WriteLine("event raised."); };
obj.Foo();
RemoveEvent<Test>(obj, "AA");
obj.Foo();
Console.ReadKey();
}
static void RemoveEvent<T>(T c, string name)
{
Delegate[] invokeList = GetObjectEventList(c, "AA");
foreach (Delegate del in invokeList)
{
typeof(T).GetEvent("AA").RemoveEventHandler(c, del);
}
}
/// <summary>
/// 获取对象事件 zgke@sina.com qq:116149
/// </summary>
/// <param name="p_Object">对象 </param>
/// <param name="p_EventName">事件名 </param>
/// <returns>委托列 </returns>
public static Delegate[] GetObjectEventList(object p_Object, string p_EventName)
{
FieldInfo _Field = p_Object.GetType().GetField(p_EventName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
if (_Field == null)
{
return null;
}
object _FieldValue = _Field.GetValue(p_Object);
if (_FieldValue != null && _FieldValue is Delegate)
{
Delegate _ObjectDelegate = (Delegate)_FieldValue;
return _ObjectDelegate.GetInvocationList();
}
return null;
}