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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…