I have an event in a generic class
Public class SomeClass
Public Event ChangeEvent(oldValue As T, newValue As T)
' some code..
End Class
And I have a complicated method to which I pass an Action(Of T)
which should get added to the event and later removed again. It would look something like the following:
Public Sub SomeSub(listener As Action(Of T, T)
AddHandler ChangeEvent, listener
' some code
RemoveHandler ChangeEvent, listener
End Sub
But on both lines the compiler gives me the following error:
Value of type 'Action(Of T, T)' cannot be converted to 'ChangeEventHandler'.
The following works, but I can't remove the handler as it was a lambda expression.
Public Sub SomeSub(listener As Action(Of T, T)
AddHandler ChangeEvent, Sub(x, y) listener(x, y)
End Sub
Is there a solution which doesn't involve me storing the lambda as a member? Please note that I cannot change the Event. I am only in control of the Method adding the listener.