日期:2014-05-16 浏览次数:21312 次
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
------解决方案--------------------
&n