//看看控件是否已经绑定某事件 public static bool isBinded(Control control, string EventName, string EventHandlerTypeName) //, string eventName, Type controlType) { if (GetComponentEventDelegate(control, EventName, EventHandlerTypeName) == null) return false; else return GetComponentEventDelegate(control, EventName, EventHandlerTypeName).Length > 0; } private static Delegate[] GetComponentEventDelegate(Component component, string EventName, string EventHandlerTypeName) { Type componentType = component.GetType(); PropertyInfo eventsPropertyInfo = componentType.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); EventHandlerList eventHanlderList = eventsPropertyInfo.GetValue(component, null) as EventHandlerList; do { FieldInfo[] fieldInfoList = componentType.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fieldInfoList) { object fieldValue = fieldInfo.GetValue(component); if (fieldValue != null) { Type fieldType = fieldValue.GetType(); if (fieldType.Name == EventHandlerTypeName && (fieldValue as Delegate) != null) { return (fieldValue as Delegate).GetInvocationList(); } } } componentType = componentType.BaseType; } while (componentType != null); return null; }
dotnet一技巧
最新推荐文章于 2013-06-13 14:31:00 发布