- //是否已经绑定了事件
- private bool IsBindEvent(Type type, Control con, string eventName)
- {
- bool isBind = false;
- PropertyInfo pi = type.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); //获取type类定义的所有事件的信息
- EventHandlerList ehl = (EventHandlerList)pi.GetValue(con, null); //获取con对象的事件处理程序列表
- FieldInfo fieldInfo = (typeof(Control)).GetField("EventText", BindingFlags.Static | BindingFlags.NonPublic); //获取Control类Click事件的字段信息
- Delegate d = ehl[fieldInfo.GetValue(null)];
- if (d == null)
- {
- return isBind;
- }
- foreach (Delegate del in d.GetInvocationList())
- {
- if (del.Method.Name == eventName)
- {
- isBind = true;
- break;
- }
- }
- return isBind;
- }
- //该片段来自于http://www.codesnippet.cn/detail/060820135000.html
来源: http://www.codesnippet.cn/detail/060820135000.html