Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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();
        }