.net - 在 .NET Framework 中引发自定义事件的不同方法

标签 .net vb.net custom-events

我知道在 VB .Net 中可以定义自定义事件处理程序。

这是一个类知道另一个类何时正在监听其事件的一种方式。此示例代码来自MSDN :

Private Events As EventHandlerList

Public Custom Event MyEvent As EventHandler
    AddHandler(value As EventHandler)
        Events.AddHandler("Test", value)
    End AddHandler

    RemoveHandler(value As EventHandler)
        Events.RemoveHandler("Test", value)
    End RemoveHandler

    RaiseEvent(sender As Object, e As EventArgs)
        CType(Events("Test"), EventHandler).Invoke(sender, e)
    End RaiseEvent
End Event

现在,您可以通过以下方式引发自定义事件:

Private Sub MySub()
    RaiseEvent MyEvent(Me, EventArgs.Empty)
End Sub

到目前为止一切顺利,没有任何问题。

我的问题是,由于在我的类中我可以直接访问 EventHandlerList,我可以在自定义事件处理程序之外调用它吗? 如果我这样做了,这个子程序的功能与上面的子程序有什么不同吗?

Private Sub MySub2()
    CType(Events("Test"), EventHandler).Invoke(Me, EventArgs.Empty)
End Sub

我知道这可能根本不是好习惯,我只是好奇,因为我可能有一个将事件名称作为 String 传递的函数,这样引发事件的方式可能适用于我,我会做类似的事情:

Private Sub RaiseCustomEvent(EventName As String, Ev as EventArgs)
    CType(Events(EventName), EventHandler).Invoke(Me, Ev)
End Sub

最佳答案

My question is, since in my class I have a direct access to the EventHandlerList, can I call it outside the custom event handler?

是的。

And if I do, is there any difference between what this sub does from the one above?

没有。 RaiseEvent 语句只是调用事件声明的 RaiseEvent 部分。 VB.NET 规范规定:

The RaiseEvent declaration takes the same parameters as the event delegate and will be called when a RaiseEvent statement is executed.

但请注意,MSDN 示例已损坏 - 如果未附加任何事件处理程序 ( Fiddle ),则会引发 NullReferenceException:

Public Module Module1
    Public Sub Main()
        Dim x As New T()
        x.RaiseTestEvent()
    End Sub
End Module

Public Class T
    Private Events As New System.ComponentModel.EventHandlerList()

    Public Custom Event MyEvent As EventHandler
        AddHandler(value As EventHandler)
            Events.AddHandler("Test", value)
        End AddHandler

        RemoveHandler(value As EventHandler)
            Events.RemoveHandler("Test", value)
        End RemoveHandler

        RaiseEvent(sender As Object, e As EventArgs)
            CType(Events("Test"), EventHandler).Invoke(sender, e)
        End RaiseEvent
    End Event

    Public Sub RaiseTestEvent()
        ' Throws NullReferenceException if no event handler is attached.
        RaiseEvent MyEvent(Me, EventArgs.Empty)
    End Sub
End Class

对于“常规”事件,如果没有附加处理程序,则 RaiseEvent 只是一个 NO-OP ( Fiddle )。 VB 规范说:

The RaiseEvent statement is processed as a call to the Invoke method of the event's delegate, using the supplied parameters, if any. If the delegate’s value is Nothing, no exception is thrown.

关于.net - 在 .NET Framework 中引发自定义事件的不同方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37748278/

相关文章:

c# - Windows 应用商店应用程序中 XAML 网格中的图像重叠

对于自定义事件,jQuery 触发器不会使用 bind() 或 on() 触发

ios - admob 中介自定义事件请求错误 : No ad to show from all configured ad networks

c# - .NET Framework 中的 DbProviderFactories.RegisterFactory?

c# - SharePoint 错误 : Web application at xxxx could not be found

mysql - vb.net无法连接mysql

sql - System.ServiceModel.FaultException SQL错误,是他们结束了吗?

javascript - 命名事件处理程序方法 "onEvent"的用例有哪些

.net - 如何在用户键入任何内容时退出控制台应用程序?

c# - 在 JSON.NET 中反序列化的转换接口(interface)