Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

reflection - Tracing all events in VB.NET

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...