Can I have a loaded assembly subscribe to events of my hosting application?

Using reflection, I'm able to generically subscribe to events happening in assemblies loaded at runtime.

if( aType.IsSubclassOf( typeof( UserControl ) ) ) { genericEvent = aType.GetEvent( "GenericEvent" ); tDelegate = genericEvent.EventHandlerType; d = Delegate.CreateDelegate( tDelegate , null , localAssmEventHandler ); //localAssm is thisExecutingAssembly addHandler = genericEvent.GetAddMethod(); object[] addHandlerArgs = { d }; addHandler.Invoke( (object) controlOfInterest , addHandlerArgs ); } 

Works beautifully. I'm wonder though, is there a way use a methodinfo to subscribe to my hosting applications 'GlobalEventOfSomeKind' just through the normal += when I create an instance of my external object (assuming I have created a function that meets the specification of my GlobalEventOfSomeKind?

That would be handy.

by toolongdontread via /r/csharp

Leave a Reply