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
639 views
in Technique[技术] by (71.8m points)

c# - How can I get an actual EventHandler delegate instance from an event in VB.NET?

In C#, I could do something like this:

EventHandler handler = this.SomeEvent;

...which would allow me to, for example, do:

Delegate[] attachedHandlers = handler.GetInvocationList();

In VB.NET, I can't seem to figure out how to do a similar thing.

This doesn't work:

Dim handler As EventHandler = Me.SomeEvent

...due to the following error:

Public Event SomeEvent(sender As Object, e As EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

But this doesn't work either:

Dim handler As EventHandler = AddressOf Me.SomeEvent

...because:

'AddressOf' operand must be the name of a method (without parentheses).

So how can I actually get an EventHandler from an event in VB.NET? The only idea that's immediately coming to mind is to use reflection, but that seems pretty ridiculous.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
   Private Event MyEvent()
   Private delegates() As System.Delegate = MyEventEvent.GetInvocationList()

undocumented, found here


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

...