2

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.

MatsT
  • 1,759
  • 3
  • 20
  • 33

1 Answers1

7

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
  • 53,986
  • 12
  • 141
  • 274
  • 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 May 10 '10 at 15:04
  • 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 May 11 '10 at 13:26
  • 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 May 11 '10 at 14:25
  • 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 May 11 '10 at 14:34
  • 1
    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 May 11 '10 at 15:36
  • 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 May 11 '10 at 18:15
  • The gojisoft link in the comment from @ChrisHaas no longer works; the domain is for sale. However, the page & zip file is still available on the Internet Archive at the following link: https://web.archive.org/web/20160420085613/http://gojisoft.com/blog/2010/04/22/event-sequence-unit-testing-part-1/ – spinjector Mar 26 '22 at 23:01