Hi there,
I tried to find a way to dynamically raise event using Reflection API. The
scenario is, I have to raise a event in a class instance by using a given
event name. I can use the GetEvent(...) method of the Reflection API to get
the EventInfo object. However, the GetRaiseMethod of the EventInfo object is
always null. And I haven't found another way other than this to dynamically
raise an event of a .NET class.
Have anyone ideas about how to do it? Any help is greatly appreciated!

Signature
KIM
Jesse Houwing - 15 Jun 2006 11:36 GMT
> Hi there,
>
[quoted text clipped - 6 lines]
>
> Have anyone ideas about how to do it? Any help is greatly appreciated!
In the Framework classes there is always one method used to invoke each
event. For example in WindowsForms Control.Validating is always
triggered by Control.NotifyValidating(), which is private or protected
by default. If you're inheriting you should make use of this method.
If you want to trigger the event from the 'outside', you should be able
to use
EventName.GetType().GetMethod("Invoke").Invoke(EventName, params);
Jesse Houwing
Barry Kelly - 15 Jun 2006 12:03 GMT
> I tried to find a way to dynamically raise event using Reflection API. The
> scenario is, I have to raise a event in a class instance by using a given
[quoted text clipped - 4 lines]
>
> Have anyone ideas about how to do it? Any help is greatly appreciated!
An event is like a property, except that instead of "get" and "set"
methods, it has "add" and "remove" methods.
If the event declaration doesn't provide custom "add" and "remove"
clauses, then the event storage is a private field of a delegate type,
the same delegate type as the event. The name of the private delegate
field is the same as the event's name.
Because the event declaration can override the "add" method and store
the delegate added to the event somewhere else (in a hash table, for
example), there is no completely general way to trigger an event from
the reflection information for that event. In particular, if you are
"outside" a class (i.e. don't have access to the internal implementation
details), you cannot invoke an event, because you can't access the
storage for the delegates that have been added to the event.
So, assuming you've got access to the internals of the class, you've got
several choices:
1) Add "add" and "remove" clauses to each event, and add all the events
to a dictionary or hashtable, using the name as a key. Lookup is then
trivial.
2) Look for the FieldInfo on the current type with the same name as the
event, get its value, and cast it to Delegate (assuming you can't cast
it to a more specific type), and then call Delegate.Invoke.
For example:
---8<---
using System;
using System.Reflection;
class App
{
static event EventHandler Foo;
static void Main()
{
Foo += delegate { Console.WriteLine("Foo!"); };
FieldInfo fooField = typeof(App).GetField("Foo",
BindingFlags.NonPublic | BindingFlags.Static);
Delegate theFoo = (Delegate) fooField.GetValue(null);
theFoo.DynamicInvoke(null, EventArgs.Empty);
}
}
--->8---
-- Barry

Signature
http://barrkel.blogspot.com/
GhostInAK - 16 Jun 2006 01:03 GMT
Hello KIM-O,
This is a terrible idea. One should never futz around in classes like this.
An event should always and only be fired from the owning object.. one should
never, unless being held at gunpoint, fire off an object's event from outside
the class. It's just bad design, bad programming manner.. bad all around.
-Boo
> Hi there,
>
[quoted text clipped - 7 lines]
>
> Have anyone ideas about how to do it? Any help is greatly appreciated!