Tracing all events in VB.NET
- by MatsT
I keep running into situations where I don't know what event I have to listen to in order to execute my code at the correct time. Is there any way to get a log of all events that is raised? Any way to filter that log based on what object raised the event?
EDIT: Final solution:
Private Sub WireAllEvents(ByVal obj As Object)
Dim parameterTypes() As Type = {GetType(System.Object), GetType(System.EventArgs)}
Dim Events = obj.GetType().GetEvents()
For Each ev In Events
Dim handler As New DynamicMethod("", Nothing, parameterTypes, GetType(main))
Dim ilgen As ILGenerator = handler.GetILGenerator()
ilgen.EmitWriteLine("Event Name: " + ev.Name)
ilgen.Emit(OpCodes.Ret)
ev.AddEventHandler(obj, handler.CreateDelegate(ev.EventHandlerType))
Next
End Sub
And yes, I know this is not a good solution when you actually want to do real stuff that triggers off the events. There are good reasons for the 1 method - 1 event approach, but this is still useful when trying to figure out which of the methods you want to add your handlers to.