当前位置 : 主页 > 编程语言 > c语言 >

在VB.net中显式添加/删除事件

来源:互联网 收集:自由互联 发布时间:2021-06-24
在C#中,您可以在订阅/取消订阅事件时定义显式添加/删除代码. 这可能在VB.net中吗? Imports System.Runtime.CompilerServices... Private propchanged As PropertyChangedEventHandler Public Custom Event PropertyChanged
在C#中,您可以在订阅/取消订阅事件时定义显式添加/删除代码.

这可能在VB.net中吗?

Imports System.Runtime.CompilerServices
...
    Private propchanged As PropertyChangedEventHandler

    Public Custom Event PropertyChanged As PropertyChangedEventHandler

        <MethodImpl(MethodImplOptions.Synchronized)> _
        AddHandler(ByVal value As PropertyChangedEventHandler)
            propchanged = DirectCast([Delegate].Combine(propchanged, value), PropertyChangedEventHandler)
        End AddHandler

        <MethodImpl(MethodImplOptions.Synchronized)> _
        RemoveHandler(ByVal value As PropertyChangedEventHandler)
            propchanged = DirectCast([Delegate].Remove(propchanged, value), PropertyChangedEventHandler)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
            Dim handler = propchanged
            If handler IsNot Nothing Then handler(sender, e)
        End RaiseEvent
    End Event
网友评论