473,769 Members | 6,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the handler of an event with Reflection

Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to
see if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.

Any idea ?

TIA
Aug 9 '06 #1
5 3608
Adding an Event Handler is process. That is, the Event Handler definition is
part of the class, but the Event Handler is not assigned to the Event until
the process is running (hence, "AddHandler " or the C# equivalent).
Therefore, you cannot discover whether an event has a handler via
Reflection. The only way to know if an Event has a handler is to check the
Event object itself. If it is null, it has no Handler assigned to it.

However, I get the feeling that you're talking about something you haven't
actually mentioned. What is the requirement you are trying to fulfill?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

"Charles Bazi" <cb***@noos.frw rote in message
news:ux******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to see
if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.

Any idea ?

TIA

Aug 9 '06 #2

"Charles Bazi" <cb***@noos.frw rote in message
news:ux******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to see
if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.
Delegate.GetInv ocationList Method
http://msdn2.microsoft.com/en-us/lib...ationlist.aspx

EG
using System;
using System.Collecti ons.Generic;
using System.Reflecti on;

namespace csTest
{
class Program
{

delegate void MyEventDelegate (object Sender, EventArgs args);
static event MyEventDelegate MyEvent;

public static void Main(string[] args)
{
MyEvent += new MyEventDelegate (Program_MyEven t);
foreach (Delegate listener in MyEvent.GetInvo cationList())
{
Console.WriteLi ne(listener.Met hod.Name);
}
}

static void Program_MyEvent (object Sender, EventArgs args)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
Aug 9 '06 #3


David Browne wrote:
"Charles Bazi" <cb***@noos.frw rote in message
news:ux******** ******@TK2MSFTN GP06.phx.gbl...
>Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to see
if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.

Delegate.GetInv ocationList Method
http://msdn2.microsoft.com/en-us/lib...ationlist.aspx

EG
using System;
using System.Collecti ons.Generic;
using System.Reflecti on;

namespace csTest
{
class Program
{

delegate void MyEventDelegate (object Sender, EventArgs args);
static event MyEventDelegate MyEvent;

public static void Main(string[] args)
{
MyEvent += new MyEventDelegate (Program_MyEven t);
foreach (Delegate listener in MyEvent.GetInvo cationList())
{
Console.WriteLi ne(listener.Met hod.Name);
}
}

static void Program_MyEvent (object Sender, EventArgs args)
{
throw new Exception("The method or operation is not implemented.");
}
}
}

Hi, this is interesting, but I'm trying to que get the InvocationList
from another class, and I don't know how to get a reference to the Event.

I have classA.cs with these code:

public event EventHandler PrintEventHandl er;

And then, classB.cs, where I need to get the GetInvocationLi st

public void Adapting (Type pType, Control pControl) {

System.Reflecti on.EventInfo[] events = pControl.GetTyp e().GetEvents() ;

// And now, I have access to PrintEventHandl er from an EventInfo, but
I'm unable to get an object of type PrintEventHandl er to get the
GetInvocationLi st method...

TIA

}
Aug 10 '06 #4
Charles Bazi wrote:
Hi, this is interesting, but I'm trying to que get the InvocationList
from another class, and I don't know how to get a reference to the
Event.
In general, this cannot be done. From the outside, an event exposed by a
class is a pair of functions: EventName_Add and EventName_Remov e. Unless
you're going to get into decompiling and interpreting the IL behind those
functions, there's simply no way to locate the delegate whose invocation
list you'd need to enumerate. Worse, there's no requirement that there even
be a delegate. The event producer may actually store a the callback
information in a structure other than a delegate (for example, it might
actually pass the delegates on to another object if it's simply forwarding
an event from a contained object).

-cd
Aug 10 '06 #5
OK, thank you.

Carl Daniel [VC++ MVP] wrote:
Charles Bazi wrote:
>Hi, this is interesting, but I'm trying to que get the InvocationList
from another class, and I don't know how to get a reference to the
Event.

In general, this cannot be done. From the outside, an event exposed by a
class is a pair of functions: EventName_Add and EventName_Remov e. Unless
you're going to get into decompiling and interpreting the IL behind those
functions, there's simply no way to locate the delegate whose invocation
list you'd need to enumerate. Worse, there's no requirement that there even
be a delegate. The event producer may actually store a the callback
information in a structure other than a delegate (for example, it might
actually pass the delegates on to another object if it's simply forwarding
an event from a contained object).

-cd

Aug 10 '06 #6

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

Similar topics

1
1571
by: Cindy Liu | last post by:
Hi Everyone, I can use .Net Reflection to know there is an event defined in a class. But I don't know how to get the signature of that event handler? Any idea? Thanks in advance!!! Cindy
6
6936
by: kurotsuke | last post by:
Hi, I'm trying to use the GetKeyName API to get the name of certain keys (in the system language). For example the names of SHIFT and RETURN keys as well as the names of same special characters. I tried to call the API GetKeyName passing the KeyCode of the KeyUP event but got no result
1
2188
by: Ivan | last post by:
subject says it all. thanks in advance. Ivan
3
7581
by: Franco, Gustavo | last post by:
How can I remove all event handler from one event without do -=? I won't explain why because is too long, and that the only option left I have right now. I have one event declared for: AppDomain.CurrentDomain.AssemblyResolve += XFUNCTION I need to remove all event handlers for the event AssemblyResolve in the
0
1684
by: Stefan Rosenthal | last post by:
Hi all, I've an OL-Style App loading MDIChild-Windows (Panes) on demand dynamicaly at runtime. Loading the Forms works fine but establishing an EventHandler (to receive information from the loaded Panes by the Container) fails with the Message "Cannot bind to ...." in the line where the Delegate should be created.
7
2582
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
0
1343
by: JimN1 | last post by:
Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is a continuation of my previous table element update question. I am now getting the above error message. I need help with the format of the SQLAdapter.Update(DSet.Tables(0)) command. I have included my code below. The line is in bold text. Imports System Imports System.Reflection Imports System.Text Imports System.Collections...
3
2366
by: wolverine | last post by:
Hi, I am injecting a javascript code into html pages and attaching some dom events to some elements via attachEvent (IE only). I just want to know that is there any chance by which my event handler not getting executed ? ie, is there some means for the web developer to prevent any further handler getting executed for the same event on the same element ? To be more clear, assume the web developer has attached function "f1()" on onclick...
4
7964
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take this. Here it goes class Product { public event EventHandler ProductChanged;
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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
9994
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,...
1
7409
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
5299
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.