//看看控件是否已经绑定某事件
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一技巧
最新推荐文章于 2025-02-05 17:58:23 发布
758

被折叠的 条评论
为什么被折叠?



