跟踪 VB.NET 中的所有事件 [英] Tracing all events in VB.NET

查看:28
本文介绍了跟踪 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?

最终解决方案:

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

是的,我知道这不是一个好的解决方案,当你真的想做一些触发事件的真实事情时.1 方法 - 1 事件方法有很好的理由,但在尝试确定要将处理程序添加到哪些方法时,这仍然很有用.

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.

推荐答案

我能想到的唯一方法是使用 反射 枚举所有事件并连接一个通用处理程序,它将是一个 PITA.

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.

编辑

这里有一个全局事件捕获例程:

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

要连接它,只需调用 WireAllEvents(Me.DataGridView1) 提供您的对象.几乎所有 MS 事件都使用 sender/e(包括 DataGridView)格式,但如果由于某种原因它不使用,我认为此代码会出错.但我只是使用 DataGridView 和 Form 对其进行了测试,它按预期工作.

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.

这篇关于跟踪 VB.NET 中的所有事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆