Hi folks ! :O)
Let's say I have the following EventHandler defined in a class :
'***
Option Explicit On
Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As
MyEventArgs)
Public NotInheritable Class MyEventArgs
Inherits EventArgs
Private m_nValue As Int32
Public Property Value() As Int32
Get
Return m_nValue
End Get
Set(ByVal Value As Int32)
m_nValue = Value
End Set
End Property
End Class
Public NotInheritable Class MyEventThrower
Public Event MyEvent As MyEventHandler
Public Sub MyThrowEventSub()
Dim e As New MyEventArgs
RaiseEvent MyEvent(Me, e)
Console.WriteLine(e.Value)
End Sub
End Class
'***
How would one use the EventInfo.AddEventHandler() method in order to catch
this event ?
Here's the sample code i tried that failed miserably :
'***
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As [Assembly] = [Assembly].LoadFile("C:\MyAssembly.dll")
Dim t As Type = a.GetType(String.Format("{0}.MyEventThrower", _
a.GetName().Name))
Dim o As Object = Activator.CreateInstance(t)
Dim ev As EventInfo = t.GetEvent("MyEvent")
Dim handler As [Delegate] = _
[Delegate].CreateDelegate(ev.EventHandlerType, _
Me,
"MyEventHandler")
ev.AddEventHandler(o, handler)
t.InvokeMember("MyThrowEventSub", _
BindingFlags.Public Or _
BindingFlags.InvokeMethod Or _
BindingFlags.Instance, _
Nothing, o, Nothing)
ev.RemoveEventHandler(o, handler)
End Sub
' this doesn't not compile since MyEventArgs is not defined..
'Public Sub MyEventHandler(ByVal sender As Object, ByVal e As
MyEventArgs)
' e.Value = 15
'End Sub
'***
Thanks in advance for any ideas.
--
Best Regards
Yanick