473,509 Members | 8,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Button.Click as EventHandler via reflection [why not possible?]

Let's start with an example: if you have:

class Test
{
public delegate int MyDelegate( int n );
event MyDelegate MyEvent;
}

then you can get MyEvent using reflection:

Test test = new Test(...);
Type t = typeof( Test );
t.InvokeMember( "MyEvent", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, test, null );

this works like a charm, you get reference to the delegate of type
MyDelegate.

Then comes the problem: why following does not work:

class MyForm : Form
{
Button b;
...
}

Type t = typeof( Button );
t.InvokeMember( "Click", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, b, null );

this example looks like a perfect copy of above one. Click event is declared
as field in Control class and thus, according to my intuition, should be
available via reflection. I would expect to get a delegate of type
EventHandler. instead an exception of missing member is thrown.

I just hope I miss something obvious. If not, I really would like to hear a
technical explanation, can be complicated, I am not afraid [I know how
delegates and events work at IL level].

Thanks in advance,
Wiktor Zychla
Nov 16 '05 #1
2 6151
Hi Wiktor,

Interesting example.
The behaviour you're observing must be due to the fact that the
Click event of the Button class isn't implemented as a delegate field.
Though events are usually implemented that way (as in your first
example) alternative ways are possible.

Paragraph 17.7.2 of the C# languague specification (the ECMA version)
talks about manually implementing event accessors:

"One situation for doing so involves the case in which the storage cost
of one field per event is not acceptable. In such cases, a class can include
event-accessor-declarations and use a private mechanism for storing the
list of event handlers."

- Magnus

"Wiktor Zychla" <us**@nospam.com.eu> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
Let's start with an example: if you have:

class Test
{
public delegate int MyDelegate( int n );
event MyDelegate MyEvent;
}

then you can get MyEvent using reflection:

Test test = new Test(...);
Type t = typeof( Test );
t.InvokeMember( "MyEvent", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, test, null );

this works like a charm, you get reference to the delegate of type
MyDelegate.

Then comes the problem: why following does not work:

class MyForm : Form
{
Button b;
...
}

Type t = typeof( Button );
t.InvokeMember( "Click", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, b, null );

this example looks like a perfect copy of above one. Click event is
declared as field in Control class and thus, according to my intuition,
should be available via reflection. I would expect to get a delegate of
type EventHandler. instead an exception of missing member is thrown.

I just hope I miss something obvious. If not, I really would like to hear
a technical explanation, can be complicated, I am not afraid [I know how
delegates and events work at IL level].

Thanks in advance,
Wiktor Zychla

Nov 16 '05 #2
> Interesting example.
The behaviour you're observing must be due to the fact that the
Click event of the Button class isn't implemented as a delegate field.
Though events are usually implemented that way (as in your first
example) alternative ways are possible.

Paragraph 17.7.2 of the C# languague specification (the ECMA version)
talks about manually implementing event accessors:

"One situation for doing so involves the case in which the storage cost
of one field per event is not acceptable. In such cases, a class can
include
event-accessor-declarations and use a private mechanism for storing the
list of event handlers."

brilliant!!! I've decompiled System.Windows.Forms to find out that this is
true: the Click event is handled via two methods: add_Click and
remove_Click. I've decompiled these methods. this is the result of applying
this knowledge to the Button example:
Type t = typeof(System.ComponentModel.Component);
System.ComponentModel.EventHandlerList eh =
(System.ComponentModel.EventHandlerList)t.InvokeMe mber( "Events",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, b, null );

now I can see all handlers assigned to the event: the EventHandlerList has
private property 'head' that points to the first handler and each handler
has 'next' property that points to next handler.

thanks for the idea Magnus!

Wiktor Zychla
Nov 16 '05 #3

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

Similar topics

14
5426
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
3
5122
by: Jason Kyle Baginski | last post by:
Here's a little test app to demonstrate a problem I'm having. It creates four buttons, each one with the different FlatStyle types available. Three of them behave exactly the same way(and the...
3
6063
by: Zürcher See | last post by:
Someone has implemented a Datagrid Button for the Windows.Form?
9
3068
by: Christopher Weaver | last post by:
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form. So far,...
2
2341
by: Liqun Xu | last post by:
Hallo NG, I created a Button with Click-Event dynamically: System.Web.UI.WebControls.Button bt_1 = new Button(); bt_1.Click += new EventHandler(bt_1_click); and I implemented the Funktion...
8
3868
by: tatemononai | last post by:
I had a beautiful script that was running, well, just beautifully. But then I decided to take a button that fired an event and place it inside a <asp:table. The event WILL NOT FIRE INSIDE THE...
0
1543
by: Diffident | last post by:
Hello All, I have an asp:button and I have tied an eventhandler to this button's click event which means that the eventhandler should be executed everytime I click the button. But to my...
21
8116
by: Ben | last post by:
Hello I have frames set up in an asp.net application and need one frame to refresh another. Seeing as events need to be registered at the time the page is sent from the server, I was wondering...
4
4511
by: Soulless | last post by:
Hi, I have a form that I need to place 3 buttons on. I would like the 3 buttons to call the same method, perhaps with different arguments. In Powerbuilder, I could create a visual button...
0
7137
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
7347
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,...
0
7416
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...
1
7073
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...
1
5062
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...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
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...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
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...

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.