473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1761
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.