Explicit add / remove for an event in VB.net

In C #, you can define explicit add / remove code when an event signed / unsubscribed.

Is this possible on VB.net?

+4
source share
2 answers
 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 
+6
source

It seems that this can be done using the special events defined here .

  ' Define the MouseDown event property. Public Custom Event MouseDown As MouseEventHandler ' Add the input delegate to the collection. AddHandler(Value As MouseEventHandler) listEventDelegates.AddHandler(mouseDownEventKey, Value) End AddHandler ' Remove the input delegate from the collection. RemoveHandler(Value As MouseEventHandler) listEventDelegates.RemoveHandler(mouseDownEventKey, Value) End RemoveHandler ' Raise the event with the delegate specified by mouseDownEventKey RaiseEvent(sender As Object, e As MouseEventArgs) Dim mouseEventDelegate As MouseEventHandler = _ listEventDelegates(mouseDownEventKey) mouseEventDelegate(sender, e) End RaiseEvent End Event 
+1
source

Source: https://habr.com/ru/post/1334934/


All Articles