473,657 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "getinvocationl ist" 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 2950
I read something about a "getinvocationl ist" 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.GetInv ocationList()
Console.WriteLi ne(d.Method)
Next
End Sub
End Class

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


You can call GetCustomAttrib utes() 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.T est11, AddressOf Me.Test

After Adding it I found the Delegate twice.

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

"Mattias Sjögren" <ma************ ********@mvps.o rg> schrieb im Newsbeitrag
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I read something about a "getinvocationl ist" 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.GetInv ocationList()
Console.WriteLi ne(d.Method)
Next
End Sub
End Class

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


You can call GetCustomAttrib utes() 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.T est11, 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
3691
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
2000
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 ofcourse). 2) Let Foo have non-static protected (or public) method Bar() and Yin have a non-static method called Yang(). 4) Add a member object to Foo of Class Yin called yinObj.
3
1877
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. There must be a better way to code this than calling OnResetTimer in all these procedures separately. Is there not a way to use one handler for all of them? private void pnlCPI_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)...
5
1435
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 add event handlers to the buttons and when they disconnect i remove them. Now, everything works fine when they connnect for the first time. All of the msgs are sent propoerly by the client. To figure out what button is click by the client i used...
3
3616
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. while eventinfo has methods for addhandler and remove handler I don't se a way to do a 'gethandler' or 'enumeratehandlers' etc.
30
3635
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 it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the underlying delegate is private, but is that all ? -- Regards
4
2812
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 RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to another library and call this list of functions. This could provide a new way to build rule base...
7
1390
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 earlier post, instead of maintaining a flag, I'm unsubscribing to this eventHandler. My question is, why is this functional: this.Activated -= new System.EventHandler(Form1_Activated); instead of:
15
8192
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 Delegate. My testing showed that actually it was six times faster: 0.5 seconds for 100,000 iterations versus 3.1 seconds. Can anyone explain why? Something in the way I coded it? I'd appreciate any insights. Here's the code (in a Windows Form...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.