views:

140

answers:

1

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.

+1  A: 

The only way that I can think of is to use Reflection to enumerate all of the events and wire up a generic handler which would be a PITA.

Is the problem with Framework events? If so, Microsoft does a pretty good job of giving event life-cycle/call order.

Edit

So here's a global event capture routine:

Private Sub WireAllEvents(ByVal obj As Object)
    'Grab all of the events for the supplied object
    Dim Events = obj.GetType().GetEvents()
    'This points to the method that we want to invoke each time
    Dim HandlerMethod = Me.GetType().GetMethod("GlobalHandler")
    'Loop through all of the events
    For Each ev In Events
        'Wire in a handler for the event
        ev.AddEventHandler(obj, [Delegate].CreateDelegate(ev.EventHandlerType, Me, HandlerMethod))
    Next
End Sub
Public Sub GlobalHandler(ByVal sender As Object, ByVal e As EventArgs)
    'Probably want to do something more meaningful here than just tracing
    Trace.WriteLine(e)
End Sub

To wire it in just call WireAllEvents(Me.DataGridView1) supplying your object. Almost all MS events use sender/e (including DataGridView) format but if for some reason it doesn't I think this code will error out. But I just tested it with both a DataGridView and Form and it worked as expected.

Chris Haas
Yes, it's regarding framework events. For example a DataGridView has a huge amount of different events. Some of them have pretty obvious triggers but the same action will often trigger many events and some have names that aren't as self-explaining. If I were to perform an action and get a list of all the events it triggered it would be a lot easier to figure out which ones i want to handle. Even better if I could break on each event to inspect the database/datasets manually.
MatsT
This seems to do what I want, except that I have yet to figure out how to get the names of the actual events rather than the types of the EventArgs.
MatsT
I tried jamming a proxy delegate into the AddEventHandler method that accepted ev.Name but I couldn't figure out the syntax. I switch to VB10 and tried using an anonymous delegate but that wouldn't run.
Chris Haas
Check out this blog post, he's got some code for event monitoring:http://gojisoft.com/blog/2010/04/22/event-sequence-unit-testing-part-1/
Chris Haas
Using the very extensive guide at http://msdn.microsoft.com/en-us/library/ms228976.aspx I was able to figure it out. Using Reflection and dynamic methods I can print custom messages. See the original question for code.
MatsT
Very cool @MatsT, every time I saw an Emit() out there I was hoping there was a better way but I guess not.
Chris Haas