开发者

Handle ALL events in one handler?

开发者 https://www.devze.com 2023-01-08 11:30 出处:网络
Is it possible in VB.NET to easily write an event handler that will handle every event that fires?I\'m wondering if one could make a logging system using something like this.

Is it possible in VB.NET to easily write an event handler that will handle every event that fires? I'm wondering if one could make a logging system using something like this.

I'm wanting to do something like (in pseudocode):

Public Sub eventHandledEvent(ByVal sender As Obj开发者_如何学Pythonect, ByVal e As EventArgs)
    File.Write(sender.EventName)
End Sub

I realize it would be slow, but it wouldn't be for a production system, only as a development tool.


You can do this with reflection. Here's how. Create a form with a textbox called TextBox1. Paste the following code. Run the project and look at the immediate window.

Public Class Form1

  Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    RegisterAllEvents(TextBox1, "MyEventHandler")
  End Sub

  Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Debug.WriteLine("An event has fired: sender= " & sender.ToString & ", e=" & e.ToString)
  End Sub
  Sub RegisterAllEvents(ByVal obj As Object, ByVal methodName As String)
    'List all events through reflection'
    For Each ei As System.Reflection.EventInfo In obj.GetType().GetEvents()
      Dim handlerType As Type = ei.EventHandlerType
      Dim method As System.Reflection.MethodInfo = Me.GetType().GetMethod(methodName)
      'Create a delegate pointing to the method'
      Dim handler As [Delegate] = [Delegate].CreateDelegate(handlerType, Me, method)
      'Register the event through reflection'
      ei.AddEventHandler(obj, handler)
    Next
  End Sub
End Class

This is from Francesco Balena's book Programming Microsoft Visual Basic 2005 The Language. The techique works with any object that raises events, not just controls. It uses contravariance.

If you buy the book, there's a full explanation and some more code which will allow you to identify which event has fired in the universal handler, and use regular expressions to handle only a subset of events. I don't feel I can post such a long excerpt here.


Edit: Adjusted due to Hans' comment.

No problem, at least for some events, since it's already built in for the events that send out messages. Just look at Control.WndProc. All messages to the window will go through there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号