Dynamically firing events using reflection.

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
T

Tim Haughton

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
T

Tim Haughton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top