博客园

winform 事件清除

static void ClearEvent(DataGridView pControl, string pEventName)

{

if (pControl == null) return;
var controlType = pControl.GetType();
var eName = ("EVENT_" + controlType.Name + pEventName).ToUpper();

if (string.IsNullOrEmpty(pEventName)) return;

BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public
| BindingFlags.Static | BindingFlags.NonPublic;//筛选
BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;

PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);

EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(pControl, null);//事件列表

FieldInfo fieldInfo = controlType.GetField(eName, mFieldFlags);
var fieldInfos = controlType.GetFields(mFieldFlags);
Delegate d = eventHandlerList[fieldInfo.GetValue(pControl)];

if (d == null) return;

EventInfo eventInfo = controlType.GetEvent(pEventName);

 

foreach (Delegate dx in d.GetInvocationList())

eventInfo.RemoveEventHandler(pControl, dx);//移除已订阅的pEventName类型事件

 

}

posted @ 2020-09-24 16:41  一心一意  阅读(929)  评论(0编辑  收藏  举报

Powered by:
一心一意
Copyright & JGuang