473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find out all Eventhandlers/Delegates using a Event via Reflection

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

Nov 20 '05 #1
3 2937
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #2

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" <ma********************@mvps.org> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 20 '05 #3
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: kode | last post by:
im having problems trying to understand the delegates in c#, does anybody know some link where i can find a good and simple explanation? thanks
10
by: Brad | last post by:
Question for all: Would one consider this a loophole in .net with respect to nested (inner) classes? 1) Create outer class Foo and create a nested class Yin (inside Foo's class definition...
3
by: Robert | last post by:
I need some assistance doing some "right way to do it" coding. The following are EventHandlers associated with Delegates in a child form that call a procedure in the MDI form that resets a timer....
5
by: Raj Chudasama | last post by:
I have a client server app. On the client i have buttons (derived from user contol) that let user perform actions when they are connected to the server. so when they connect (using a menuitem) i...
3
by: Phil | last post by:
Does any body know how to get a list of event handlers assigned to a paticular event? I'd like to be able to determine if a sub has already been added as an event handler for a form.paint event....
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
7
by: RvGrah | last post by:
I'm using the Activated event of my form to run a database query in a backroundWorker, and want to dismiss the call after the first time the form is activated. Based on some excellent advice in an...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.