Just a simple example which raises event by its name, hope it will be useful to some one, not sure :(
Public Class car Public Event Exploded(ByVal e As EventArgs, ByVal Message As String) 'please use suggested declaration, just keeping it simple Private _acc As Integer Public Sub Accelarate(ByVal speed As Integer) _acc += speed If _acc > 50 Then RaiseEventByName("Exploded") End If End Sub
Public Sub RaiseEventByName(ByVal handler As String) Dim delEvent As MulticastDelegate = DirectCast(Me.[GetType]().GetField(handler & "Event", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic).GetValue(Me), MulticastDelegate) Dim AllDelegates As [Delegate]() = delEvent.GetInvocationList() For Each dlg As [Delegate] In AllDelegates dlg.Method.Invoke(dlg.Target, New Object() {New EventArgs(), "Oops, you have blown your car"}) Next End Sub End Class
Lets Use It
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As New car AddHandler c.Exploded, AddressOf exploded c.Accelarate(60)
End Sub Private Sub exploded(ByVal e As EventArgs, ByVal Message As String) MessageBox.Show(Message) End Sub |
8d82d5df-0484-4c1d-9abe-488eb9e51ac8|0|.0