查找 .NET 中 Windows 窗体控件的所有事件处理程序

发布于 2024-09-26 04:07:09 字数 124 浏览 1 评论 0原文

有没有办法找到 Windows 窗体 控件的所有事件处理程序?具体静态定义的事件处理程序?

Is there a way to find all event handlers for a Windows Forms control? Specifically statically defined event handlers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天煞孤星 2024-10-03 04:07:09

Windows 窗体有强有力的对策来防止这样做。大多数控件将事件处理程序引用存储在需要秘密“cookie”的列表中。 cookie 值是动态创建的,您无法预先猜测它。反射是一个后门,你必须知道cookie变量名。例如,Control.Click 事件的名称为“EventClick”,您可以在参考源中或使用 Reflector 看到这一点。

这一切都是非常不切实际的,如果你感觉自己在做一些不明智的事情,那么你就走在正确的轨道上。您可以在我的答案中找到执行此操作的示例代码 此线程

Windows Forms has strong counter-measures against doing this. Most controls store the event handler reference in a list that requires a secret 'cookie'. The cookie value is dynamically created, you cannot guess it up front. Reflection is a backdoor, you have to know the cookie variable name. The one for the Control.Click event is named "EventClick" for example, you can see this in the Reference Source or with Reflector.

This is all incredibly unpractical, if you're getting the feeling you are doing something unwise then you're on the right track. You can find sample code that does this in my answer in this thread.

浮光之海 2024-10-03 04:07:09

Windows Forms controls use an EventHandlerList property called Events to hold event handlers so you could iterate that collection. To determine which subscribed handlers are static, you will need to use reflection.

牵强ㄟ 2024-10-03 04:07:09

此代码将获取控件的事件处理程序

Private Function getEventHandlers(ctrl As Control) As System.ComponentModel.EventHandlerList
    Dim value As System.ComponentModel.EventHandlerList = Nothing
    Try
        Dim propInfo As System.Reflection.PropertyInfo = GetType(Control).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static)
        If propInfo IsNot Nothing Then value = CType(propInfo.GetValue(ctrl), System.ComponentModel.EventHandlerList)
    Catch ex As Exception
    End Try
    Return value
End Function

我在多次添加事件处理程序时遇到问题,导致多个引发事件。下面将允许您检查控件是否已经具有指定事件的处理程序。

Private Function hasEventHandler(ctrl As Control, Optional eventName As String = "Click") As Boolean
    Dim value As Boolean = False
    Try
        Dim handlerList As System.ComponentModel.EventHandlerList = getEventHandlers(ctrl)
        Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField("Event" + eventName, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
        If controlEventInfo IsNot Nothing Then
            Dim eventKey As Object = controlEventInfo.GetValue(ctrl)
            Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey)
            If EventHandlerDelegate IsNot Nothing Then value = True
        End If
    Catch ex As Exception
    End Try
    Return value
End Function

This code will get the event handlers for a control

Private Function getEventHandlers(ctrl As Control) As System.ComponentModel.EventHandlerList
    Dim value As System.ComponentModel.EventHandlerList = Nothing
    Try
        Dim propInfo As System.Reflection.PropertyInfo = GetType(Control).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static)
        If propInfo IsNot Nothing Then value = CType(propInfo.GetValue(ctrl), System.ComponentModel.EventHandlerList)
    Catch ex As Exception
    End Try
    Return value
End Function

I have had problems with adding event handlers more than once resulting in multiple raised events. The following will allow you to check whether a control already has a handler for a specified event.

Private Function hasEventHandler(ctrl As Control, Optional eventName As String = "Click") As Boolean
    Dim value As Boolean = False
    Try
        Dim handlerList As System.ComponentModel.EventHandlerList = getEventHandlers(ctrl)
        Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField("Event" + eventName, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
        If controlEventInfo IsNot Nothing Then
            Dim eventKey As Object = controlEventInfo.GetValue(ctrl)
            Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey)
            If EventHandlerDelegate IsNot Nothing Then value = True
        End If
    Catch ex As Exception
    End Try
    Return value
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文