473,396 Members | 1,693 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
Nov 16 '05 #1
4 7131
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

Nov 16 '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.


Nov 16 '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.



Nov 16 '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.

Nov 16 '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...
5
by: MS Newsgroups | last post by:
Hi, I have a scenario where I am dynamically adding a control from code when a controls event is fired. The problem I have is that when the newly created control is clicked, the click event does...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...

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.