移除类型中的所有事件:
/// <summary>
/// 清除事件
/// </summary>
/// <param name="obj"></param>
public static void RemoveAllEvent(object obj)
{
var newType = obj.GetType();
foreach (var item in newType.GetEvents())
{
FieldInfo _Field = newType.GetField(item.Name, BindingFlags.Instance | BindingFlags.NonPublic);
if (_Field != null)
{
object _FieldValue = _Field.GetValue(obj);
if (_FieldValue != null && _FieldValue is Delegate)
{
Delegate _ObjectDelegate = (Delegate)_FieldValue;
Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
if (invokeList != null)
{
foreach (Delegate del in invokeList)
{
item.RemoveEventHandler(obj, del);
}
}
}
}
}
}