473,508 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically firing events using reflection.

Can anyone tell me how to dynamically fire an event?

I have an object that reflects on which events it has and based on some
logic, elects which one to fire. Or at least that's the plan.

So the question is - how do I fire an event when I have an EventInfo
object??

There is a GetRaiseMethod() method, but that's not the one for me methinks.
Anyone have any offers?

Cheers,

Tim H
Jul 21 '05 #1
4 1768
Tim,

What you want to do is call the GetRaiseMethod on the EventInfo. This
returns a MethodInfo instance which you can then call Invoke on to raise the
event.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Haughton" <ti******************@omnibus.SPAMco.uk> wrote in message
news:ig**********************@news.easynews.com...
Can anyone tell me how to dynamically fire an event?

I have an object that reflects on which events it has and based on some
logic, elects which one to fire. Or at least that's the plan.

So the question is - how do I fire an event when I have an EventInfo
object??

There is a GetRaiseMethod() method, but that's not the one for me methinks. Anyone have any offers?

Cheers,

Tim H

Jul 21 '05 #2
That's the avenue down which I started. But I hit a problem as this method
returns null every time.

Given that there can be more than one handler for an event, precisely which
method is returned?

Cheers,

Tim H

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Og****************@TK2MSFTNGP11.phx.gbl...
Tim,

What you want to do is call the GetRaiseMethod on the EventInfo. This
returns a MethodInfo instance which you can then call Invoke on to raise the event. [Snip] "Tim Haughton" <ti******************@omnibus.SPAMco.uk> wrote in message
news:ig**********************@news.easynews.com...
Can anyone tell me how to dynamically fire an event?

I have an object that reflects on which events it has and based on some
logic, elects which one to fire. Or at least that's the plan.

So the question is - how do I fire an event when I have an EventInfo
object??

There is a GetRaiseMethod() method, but that's not the one for me methinks.


Jul 21 '05 #3
Tim,

Try as I might, I can not figure out how to do this. The only reason I
can think of such an omission is the fact that only event owners (the type
they are defined in) are supposed to fire them. Reflection shouldn't
provide a way around it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Haughton" <ti******************@omnibus.SPAMco.uk> wrote in message
news:40***********************@news.easynews.com.. .
That's the avenue down which I started. But I hit a problem as this method
returns null every time.

Given that there can be more than one handler for an event, precisely which method is returned?

Cheers,

Tim H

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Og****************@TK2MSFTNGP11.phx.gbl...
Tim,

What you want to do is call the GetRaiseMethod on the EventInfo. This returns a MethodInfo instance which you can then call Invoke on to raise

the
event.

[Snip]
"Tim Haughton" <ti******************@omnibus.SPAMco.uk> wrote in message
news:ig**********************@news.easynews.com...
Can anyone tell me how to dynamically fire an event?

I have an object that reflects on which events it has and based on some logic, elects which one to fire. Or at least that's the plan.

So the question is - how do I fire an event when I have an EventInfo
object??

There is a GetRaiseMethod() method, but that's not the one for me

methinks.



Jul 21 '05 #4
A bit of further scouting on google on the topic of GetRaiseMethod returning
null seems to show that whether of not an event has an associated Raise or
Fire method is language and may be even compiler dependant. The MS C#
compiler does not create this method.

This willmean I'm stuck with some truly horrid code, but it's not a show
stopper. We live and learn. Thanks for your input Nicholas.

Regards,

Tim Haughton

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ot****************@tk2msftngp13.phx.gbl...
Tim,

Try as I might, I can not figure out how to do this. The only reason I can think of such an omission is the fact that only event owners (the type
they are defined in) are supposed to fire them. Reflection shouldn't
provide a way around it.

Hope this helps.

Jul 21 '05 #5

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

Similar topics

4
7138
by: Tim Haughton | last post by:
Can anyone tell me how to dynamically fire an event? I have an object that reflects on which events it has and based on some logic, elects which one to fire. Or at least that's the plan. So...
1
8135
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
1
1623
by: Bob | last post by:
I have a requirement to show a label and x number of buttons in all of the pages. So i decided to write a Base Page class and added code for dynamically adding the label and buttons. For the...
3
4293
by: JoakimR | last post by:
Hello, I have a problem with events not firing. I've created a web user control which renders a calendar using a table control. In two cells I'm adding LinkButtons for "prev/next month". However,...
5
4374
by: bryanp10 | last post by:
I have a page that is almost entirely dynamically created. Textboxes and checkbox are working fine, firing events, and persistent their state. DropDownList is giving me a major headache. All...
1
1483
by: AndrewMBaldwin | last post by:
Ok, this is going to be a long post, so I apologize in advance, but if it was an easy question I would have probably found an answer somewhere out here by now... The short story of this is that...
0
1448
by: yohaas | last post by:
I am dynamically adding user control to a page using Controls.Add(control). It seems that the Page_Load as well as the other page rendering events (Init, PreRender etc...) get fired twice. I...
5
4399
by: kmcmanus | last post by:
I have just started to write a few business classes that are largely made up of properties (getters and setters). For each setter I want to fire a changed event - each event will have a unique...
2
2644
by: mswlogo | last post by:
I looked high and low for code to do this and finally found some VB code that did it right. This is a C# flavor of it. public event EventHandler<EventArgsMyEventToBeFired; public void...
0
7225
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
7124
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
7326
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,...
1
7046
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...
0
7498
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...
1
5053
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
4707
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
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
418
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.