Dynamically firing events using reflection.

  • Thread starter Thread starter Tim Haughton
  • Start date Start date
T

Tim Haughton

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
 
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.
 
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 said:
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
[Snip]
Tim Haughton said:
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.
 
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]
- (e-mail address removed)

Tim Haughton said:
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

message news:Ogl1%[email protected]...
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
[Snip]
Tim Haughton said:
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.
 
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
 
Back
Top