2010-11-05 28 views
5

在C#中,我可以做這樣的事情:如何從VB.NET中的事件獲取實際的EventHandler委託實例?

EventHandler handler = this.SomeEvent; 

...這會允許我,例如,做:

Delegate[] attachedHandlers = handler.GetInvocationList(); 

在VB.NET,不是我似乎能弄清楚如何做類似的事情。

這不起作用:

Dim handler As EventHandler = Me.SomeEvent 

...由於以下錯誤:

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.

但是,這並不工作之一:

Dim handler As EventHandler = AddressOf Me.SomeEvent 

.. 。因此:

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

那麼我怎麼能從VB.NET中的事件中獲得EventHandler?立即想到的唯一想法是使用反射,但這看起來很荒謬。

回答

6
Private Event MyEvent() 
    Private delegates() As System.Delegate = MyEventEvent.GetInvocationList() 

無證,發現here

+0

不錯。從來沒有見過我以前沒有看到過 – jasper 2010-11-05 17:39:55

+1

!似乎是一個「不要讓,一個VB.Net開發人員看到」的事情 – 2010-11-05 17:51:08

+0

哦,親愛的上帝...... – 2010-11-05 18:18:23