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.In stance | BindingFlags.Ge tField |
BindingFlags.No nPublic, 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.In stance | BindingFlags.Ge tField |
BindingFlags.No nPublic, 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 2 6167
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.co m.eu> wrote in message
news:u1******** ******@TK2MSFTN GP10.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.In stance | BindingFlags.Ge tField | BindingFlags.No nPublic, 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.In stance | BindingFlags.Ge tField | BindingFlags.No nPublic, 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
> 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.C omponentModel.C omponent);
System.Componen tModel.EventHan dlerList eh =
(System.Compone ntModel.EventHa ndlerList)t.Inv okeMember( "Events",
BindingFlags.Ge tProperty | BindingFlags.In stance | BindingFlags.No nPublic,
null, b, null );
now I can see all handlers assigned to the event: the EventHandlerLis t 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
that works well as an html page.
It brings up a modal popup window that I have been trying to work out for
days now and this was the closest I have been able to come.
I added a little asp.net code and an asp.net button and cannot get it to
|
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 way I'd
anticipate), but the FlatStyle.System one does not.
If you click on any of the buttons, use the enter key, or the
spacebar, they will bring up a messagebox that says "Activate".
Except the FlatStyle.System one, which will bring up _two_...
|
by: Zürcher See |
last post by:
Someone has implemented a Datagrid Button for the Windows.Form?
|
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, thanks to another programmer, I've got this working out quite nicely
for the properties:
Type ctrlType = subject.GetType();
ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);
|
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 bt_1_click in which I created a second
Button dynamically too.
System.Web.UI.WebControls.Button bt_2= new Button();
| |
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 TABLE!?! When I move
the button outside the table, it works just fine. Inside the table, it
doesn't. What gives?
|
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 frustration, the eventhandler does not get executed
everytime I click the button. I have to click the button twice at times to
make sure that the event handler is being called. Why is the control behaving
in this way? This problem is not specific for a...
|
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 if I could place a hidden button in a frame
that would have the attribute to refresh the other. I would need code to
"invoke" the onclick event (ie i need code to click the button).
is this possible?
thanks.
|
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 object and code for
it, then inherit from that and place it on my form/window.
Does C# have anything like this? Is this possible?
|
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...
|
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,...
| |
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |