Find out all Eventhandlers/Delegates using a Event via Reflection

A

Armin

Hello

I have a UserControl with a Click Event.

Is it possible to find out the List of all Delegates/Eventhandlers using the
Event.

I read something about a "getinvocationlist" Methode for Delegates
which can get this back, but couldN#t found out how it works.

In Addition the Delegate/Eventhandler may have a Attributes.
How could I read this.

Thanx for any Help

Jerry
 
M

Mattias Sjögren

I read something about a "getinvocationlist" Methode for Delegates
which can get this back, but couldN#t found out how it works.

If you define an event called Foo, you'll get a private delegate field
called FooEvent that you can query for the handlers. Like this

Class EventPub
Public Event Foo()

Public Sub ListEm()
For Each d As [Delegate] In FooEvent.GetInvocationList()
Console.WriteLine(d.Method)
Next
End Sub
End Class

In Addition the Delegate/Eventhandler may have a Attributes.
How could I read this.

You can call GetCustomAttributes() on the MethodInfo reference you get
from Delegate.Method.



Mattias
 
F

Ford Prefect alias Armin

Cool !!

That's it

Thank you very much :)

By the way:
Very strange thing is that I get all delegates even if there are not added
at the time I call the
whole thing.


If I dynamicly Add a delegate at runtime:

Before I add it the delegate is in the list

AddHandler UserControl11.Test11, AddressOf Me.Test

After Adding it I found the Delegate twice.

Whatever...... That helps me out :) :)

Mattias Sjögren said:
I read something about a "getinvocationlist" Methode for Delegates
which can get this back, but couldN#t found out how it works.

If you define an event called Foo, you'll get a private delegate field
called FooEvent that you can query for the handlers. Like this

Class EventPub
Public Event Foo()

Public Sub ListEm()
For Each d As [Delegate] In FooEvent.GetInvocationList()
Console.WriteLine(d.Method)
Next
End Sub
End Class

In Addition the Delegate/Eventhandler may have a Attributes.
How could I read this.

You can call GetCustomAttributes() on the MethodInfo reference you get
from Delegate.Method.



Mattias
 
M

Mattias Sjögren

By the way:
Very strange thing is that I get all delegates even if there are not added
at the time I call the
whole thing.


If I dynamicly Add a delegate at runtime:

Before I add it the delegate is in the list

AddHandler UserControl11.Test11, AddressOf Me.Test

After Adding it I found the Delegate twice.

Whatever...... That helps me out :) :)


Is UserControl11 declared WithEvents? If so, the event handlers are
automatically hooked up for you.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top