reflection event enumeration

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Can someone point me to a resource that demonstrates how
to enumerate through public events (and delegates)
retrieving the names for a class?

My objective is as follows:
a) at runtime, input an assembly name and instantiate an
object of that type (this works).
b) enumerate through the public events on that object and
using the name for each event instantiate a delegate and
event info object, adding a local event handler with the
same signature.

I can do this if I know the name of the event, but I have
yet to find a way to enumerate through events and
delegates.

thx
Rob
 
Rob,

Somehting like this?

object o = someObjectRaisingEvents;
Type t = o.GetType();
foreach ( EventInfo ei in t.GetEvents() ) {
// Add code here to check whether you're interested in this event
// and verify that your handler method has the right signature.

// Create the delegate
Delegate handler = Delegate.CreateDelegate( ei.EventHandlerType,
this, "YourHandlerMethod" );
// Hook up the event
t.AddEventHandler( o, handler );
}



Mattias
 
Back
Top