获取在自定义用户控件中声明的事件的所有事件处理程序

获取在自定义用户控件中声明的事件的所有事件处理程序

问题描述:

我正在尝试编写一个通用函数,该函数给出了对控件/组件的引用以及在其类上声明的事件的名称,它应该能够(通过 Reflection )进行检索当前为指定事件名称注册的所有事件处理程序.

I'm trying to write a universal function that, given a reference to a control/component and the name of an event declared on its class, it should be able to retrieve (through Reflection) all the event-handlers currently registered for the specified event name.

我遇到的第一个也是主要的问题(已解决,因此您可以忽略此段)是,我在*中发现的所有解决方案(大多是用C#编写)都受到作者只能寻找的意义的限制.System.Windows.Forms.Control 类中的事件字段声明,因此,例如,当尝试检索 System.Windows.Forms.ToolStripMenuItem的事件处理程序时,该事件将失败..MouseEnter 事件(因为事件字段是在 System.Windows.Forms.ToolStripItem 类中声明的),并且也不考虑 System的事件字段命名.Windows.Forms.Form 类,具有下划线.因此,我涵盖了所有这些内容,目前,我的解决方案适用于(或我认为它适用于)从 System.ComponentModel.Component 继承的任何类.

The first and main problem I had (which is solved, so you can ignore this paragraph), is that all the solutions (mostly written in C#) that I found in * are limited in the meaning that the authors only look for the event-field declaration in the System.Windows.Forms.Control class, and for that reason will fail for example when trying to retrieve the event-handlers of System.Windows.Forms.ToolStripMenuItem.MouseEnter event (since the event-field is declared in System.Windows.Forms.ToolStripItem class), and also does not take into account event-fields naming of System.Windows.Forms.Form class, which have a underscore. So I covered all this, and currently my solution works (or I think it works) for any class that inherits from System.ComponentModel.Component.

我现在遇到的唯一问题是当我声明自定义类型(继承自 Control / UserControl / Component / Form 等类),然后将该类型传递给函数.在这种情况下,我会得到一个空引用异常.不知道我在这里做错了什么...

The only problem I'm having now is when I declare a custom type (that inherits from Control / UserControl / Component / Form etc. class) and I pass that type to my function. In this circumstance I get a null-reference exception. Not sure what I'm doing wrong here...

Public Shared Function GetEventHandlers(component As IComponent, eventName As String) As IReadOnlyCollection(Of [Delegate])

    Dim componentType As Type
    Dim declaringType As Type ' The type on which the event is declared.
    Dim eventInfo As EventInfo
    Dim eventField As FieldInfo = Nothing
    Dim eventFieldValue As Object
    Dim eventsProp As PropertyInfo
    Dim eventsPropValue As EventHandlerList
    Dim eventDelegate As [Delegate]
    Dim invocationList As [Delegate]()

    ' Possible namings for an event field.
    Dim eventFieldNames As String() =
            {
                $"Event{eventName}",            ' Fields declared in 'System.Windows.Forms.Control' class.
                $"EVENT_{eventName.ToUpper()}", ' Fields declared in 'System.Windows.Forms.Form' class.
                $"{eventName}Event"             ' Fields auto-generated.
            }

    Const bindingFlagsEventInfo As BindingFlags =
              BindingFlags.ExactBinding Or
              BindingFlags.Instance Or
              BindingFlags.NonPublic Or
              BindingFlags.Public Or
              BindingFlags.Static

    Const bindingFlagsEventField As BindingFlags =
              BindingFlags.DeclaredOnly Or
              BindingFlags.ExactBinding Or
              BindingFlags.IgnoreCase Or
              BindingFlags.Instance Or
              BindingFlags.NonPublic Or
              BindingFlags.Static

    Const bindingFlagsEventsProp As BindingFlags =
              BindingFlags.DeclaredOnly Or
              BindingFlags.ExactBinding Or
              BindingFlags.Instance Or
              BindingFlags.NonPublic

    Const bindingFlagsEventsPropValue As BindingFlags =
              BindingFlags.Default

    componentType = component.GetType()
    eventInfo = componentType.GetEvent(eventName, bindingFlagsEventInfo)
    If (eventInfo Is Nothing) Then
        Throw New ArgumentException($"Event with name '{eventName}' not found in type '{componentType.FullName}'.", NameOf(eventName))
    End If

    declaringType = eventInfo.DeclaringType

    For Each name As String In eventFieldNames
        eventField = declaringType.GetField(name, bindingFlagsEventField)
        If (eventField IsNot Nothing) Then
            Exit For
        End If
    Next name

    If (eventField Is Nothing) Then
        Throw New ArgumentException($"Field with name 'Event{eventName}', 'EVENT_{eventName.ToUpper()}' or '{eventName}Event' not found in type '{declaringType.FullName}'.", NameOf(eventName))
    End If

#If DEBUG Then
    Debug.WriteLine($"Field with name '{eventField.Name}' found in type '{declaringType.FullName}'")
#End If

    eventFieldValue = eventField.GetValue(component)
    eventsProp = GetType(Component).GetProperty("Events", bindingFlagsEventsProp, Type.DefaultBinder, GetType(EventHandlerList), Type.EmptyTypes, Nothing)
    eventsPropValue = DirectCast(eventsProp.GetValue(component, bindingFlagsEventsPropValue, Type.DefaultBinder, Nothing, CultureInfo.InvariantCulture), EventHandlerList)
    eventDelegate = eventsPropValue.Item(eventFieldValue)
    invocationList = eventDelegate.GetInvocationList()

    If (invocationList Is Nothing) Then ' There is no event-handler registered for the specified event.
        Return Enumerable.Empty(Of [Delegate]).ToList()
    End If

    Return invocationList

End Function

此行发生异常:

invocationList = eventDelegate.GetInvocationList()

因为 eventDelegate 为空.

要测试该异常,可以以此类为例:

To test the exception, you can take this class as example:

Public Class TestUserControl : Inherits UserControl

    Event TestEvent As EventHandler(Of EventArgs)

    Overridable Sub OnTestEvent(e As EventArgs)
        If (Me.TestEventEvent IsNot Nothing) Then
            RaiseEvent TestEvent(Me, e)
        End If
    End Sub

End Class

还有这样的用法示例:

Dim ctrl As New TestUserControl()

AddHandler ctrl.TestEvent, Sub()
                               Debug.WriteLine("Hello World!")
                           End Sub

Dim handlers As IReadOnlyCollection(Of [Delegate]) = 
    GetEventHandlers(ctrl, NameOf(TestUserControl.TestEvent))

For Each handler As [Delegate] In handlers
    Console.WriteLine($"Method Name: {handler.Method.Name}")
Next

不确定是否是与绑定标志有关的问题,或者事件字段的命名...但是在与任何内置控件/组件尝试相同时,我没有此空引用对象问题暴露事件的类,而不是该 TestUserControl 类.

Not sure if maybe it is an issue related to the binding flags, or maybe the event field naming... but I don't have this null-reference object issue when trying the same with any built-in control/component class that expose events, instead of that TestUserControl class.

我做错了什么?如何解决?请注意,此功能仍应是通用的.

What I'm doing wrong?, and how do I fix it?. Please note that this function should still be universal.

感谢@ Hans Passant 在主要问题的评论中提出的建议,该方法可以正常工作:

Thanks to what @Hans Passant suggested in his commentary in the main question, this is working as expected:

Public Function GetEventHandlers(component As IComponent, eventName As String) As IReadOnlyCollection(Of [Delegate])

    Dim componentType As Type = component.GetType()

    ' Find event declaration in the source type.
    Dim eventInfo As EventInfo = componentType.GetEvent(eventName, BindingFlags.ExactBinding Or BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static)
    If (eventInfo Is Nothing) Then
        Throw New ArgumentException($"Event with name '{eventName}' not found in type '{componentType.FullName}'.", NameOf(eventName))
    End If

    ' The type on which the event is declared.
    Dim declaringType As Type = eventInfo.DeclaringType

    ' Find event-field declaration in the declaring type.
    Dim eventField As FieldInfo = Nothing

    ' Possible namings for an event field.
    Dim eventFieldNames As String() = {
        $"Event{eventName}",            ' Fields declared in 'System.Windows.Forms.Control' class.
        $"EVENT_{eventName.ToUpper()}", ' Fields declared in 'System.Windows.Forms.Form' class.
        $"{eventName}Event"             ' Fields (auto-generated) declared in other classes.
    }

    For Each name As String In eventFieldNames
        eventField = declaringType.GetField(name, BindingFlags.DeclaredOnly Or BindingFlags.ExactBinding Or BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Static)
        If (eventField IsNot Nothing) Then
            Exit For
        End If
    Next name

    If (eventField Is Nothing) Then
        Throw New ArgumentException($"Field with name '{String.Join("' or '", eventFieldNames)}' not found in declaring type '{declaringType.FullName}'.", NameOf(eventName))
    End If

#If DEBUG Then
    Debug.WriteLine($"Field with name '{eventField.Name}' found in declaring type '{declaringType.FullName}'")
#End If

    Dim eventFieldValue As object = eventField.GetValue(component)
    If TypeOf eventFieldValue Is MulticastDelegate
        ' See @Hans Passant comment:
        ' https://*.com/questions/56763972/get-all-the-event-handlers-of-a-event-declared-in-a-custom-user-control?noredirect=1#comment100177090_56763972

        Return DirectCast(eventFieldValue, MulticastDelegate).GetInvocationList()
    End If

    Dim eventsProp As PropertyInfo = GetType(Component).GetProperty("Events", BindingFlags.DeclaredOnly Or BindingFlags.ExactBinding Or BindingFlags.Instance Or BindingFlags.NonPublic, Type.DefaultBinder, GetType(EventHandlerList), Type.EmptyTypes, Nothing)
    Dim eventsPropValue As EventHandlerList = DirectCast(eventsProp.GetValue(component, BindingFlags.Default, Type.DefaultBinder, Nothing, CultureInfo.InvariantCulture), EventHandlerList)
    Dim eventDelegate As [Delegate] = eventsPropValue.Item(eventFieldValue)
    Dim invocationList As [Delegate]() = eventDelegate?.GetInvocationList()

    If (invocationList Is Nothing) Then ' There is no event-handler registered for the specified event.
       Return Enumerable.Empty(Of [Delegate]).ToList()
    End If

    Return invocationList

End Function

我们还可以为 EventInfo 类型定义下一个方法扩展,以充当

Also we can define the next method extension for EventInfo type to act as a method overload for EventInfo.RemoveEventHandler(Object, Delegate):

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Removes an event handler from an event source.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Public Class Form1
''' 
'''     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
'''         Dim target As Form = Me
'''         Dim eventInfo As EventInfo = target.GetType().GetEvent(NameOf(Form.Click))
'''         eventInfo.RemoveEventHandler(target, NameOf(Me.Form1_Click))
'''     End Sub
''' 
'''     Private Sub Form1_Click(sender As Object, e As EventArgs) Handles MyBase.Click
'''         MsgBox(MethodBase.GetCurrentMethod().Name)
'''     End Sub
''' 
''' End Class
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="eventInfo">
''' The event information.
''' </param>
''' 
''' <param name="target">
''' The event source.
''' </param>
''' 
''' <param name="handlerName">
''' The name of the delegate to be disassociated from the events raised by <paramref name="target"/>.
''' <para></para>
''' Note that the name is case-sensitive.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<Extension>
Public Sub RemoveEventHandler(eventInfo As EventInfo, target As IComponent, handlerName As String)

    If String.IsNullOrWhiteSpace(handlerName)
        Throw New ArgumentNullException(NameOf(handlerName))
    End If

    For each handler As [Delegate] in GetEventHandlers(target, eventInfo.Name)
        If handler.Method.Name.Equals(handlerName, StringComparison.Ordinal)
            eventInfo.RemoveEventHandler(target, handler)
            Exit Sub
        End If
    Next handler
    Throw New ArgumentException($"No delegate was found with the specified name: '{handlerName}'", NameOf(handlerName))

End Sub