public void AddEventHandlersUsingReflectionTo(TypeWithEvent typeWithEvent) { EventInfo eventInfo = typeWithEvent.GetType().GetEvent("SimpleEvent"); MethodInfo staticHandlerInfo = this.GetType().GetMethod("StaticHandler", BindingFlags.Static | BindingFlags.NonPublic); eventInfo.AddEventHandler(typeWithEvent, Delegate.CreateDelegate(typeof(Action), this, "InstanceHandler")); eventInfo.AddEventHandler(typeWithEvent, Delegate.CreateDelegate(typeof(Action), staticHandlerInfo)); }
static void Main(string[] args) { TypeWithEvent typeWithEvent = new TypeWithEvent(); TypeWithHandlers typeWithHandlers = new TypeWithHandlers(); typeWithEvent.SimpleEvent += () => Console.WriteLine("Pam-pam 1"); typeWithEvent.SimpleEvent += () => Console.WriteLine("Pam-pam 2"); typeWithHandlers.AddEventHandlersUsingReflectionTo(typeWithEvent); typeWithEvent.TriggerEvent(); }