C# Dynamic Event Subscription

前端 未结 9 1859
無奈伤痛
無奈伤痛 2020-12-01 03:09

How would you dynamically subscribe to a C# event so that given a Object instance and a String name containing the name of the event, you subscribe to that event and do some

相关标签:
9条回答
  • 2020-12-01 03:44

    Do you mean something like:

    //reflect out the method to fire as a delegate
    EventHandler eventDelegate = 
       ( EventHandler ) Delegate.CreateDelegate(
           typeof( EventHandler ),    //type of event delegate
           objectWithEventSubscriber, //instance of the object with the matching method
           eventSubscriberMethodName, //the name of the method
           true );
    

    This doesn't do the subscription, but will give to the method to call.

    Edit:

    Post was clarified after this answer, my example won't help if you don't know the type.

    However all events in .Net should follow the default event pattern, so as long as you've followed it this will work with the basic EventHandler.

    0 讨论(0)
  • 2020-12-01 03:46

    This method adds to an event, a dynamic handler that calls a method OnRaised, passing the event parameters as an object array:

    void Subscribe(object source, EventInfo ev)
    {
        var eventParams = ev.EventHandlerType.GetMethod("Invoke").GetParameters().Select(p => Expression.Parameter(p.ParameterType)).ToArray();
        var eventHandler = Expression.Lambda(ev.EventHandlerType,
            Expression.Call(
                instance: Expression.Constant(this),
                method: typeof(EventSubscriber).GetMethod(nameof(OnRaised), BindingFlags.NonPublic | BindingFlags.Instance),
                arg0: Expression.Constant(ev.Name),
                arg1: Expression.NewArrayInit(typeof(object), eventParams.Select(p => Expression.Convert(p, typeof(object))))),
            eventParams);
        ev.AddEventHandler(source, eventHandler.Compile());
    }
    

    OnRaised has this signature:

    void OnRaised(string name, object[] parameters);
    
    0 讨论(0)
  • 2020-12-01 03:49

    You can compile expression trees to use void methods without any arguments as event handlers for events of any type. To accommodate other event handler types, you have to map the event handler's parameters to the events somehow.

     using System;
     using System.Linq;
     using System.Linq.Expressions;
     using System.Reflection;
    
     class ExampleEventArgs : EventArgs
     {
        public int IntArg {get; set;}
     }
    
     class EventRaiser
     { 
         public event EventHandler SomethingHappened;
         public event EventHandler<ExampleEventArgs> SomethingHappenedWithArg;
    
         public void RaiseEvents()
         {
             if (SomethingHappened!=null) SomethingHappened(this, EventArgs.Empty);
    
             if (SomethingHappenedWithArg!=null) 
             {
                SomethingHappenedWithArg(this, new ExampleEventArgs{IntArg = 5});
             }
         }
     }
    
     class Handler
     { 
         public void HandleEvent() { Console.WriteLine("Handler.HandleEvent() called.");}
         public void HandleEventWithArg(int arg) { Console.WriteLine("Arg: {0}",arg);    }
     }
    
     static class EventProxy
     { 
         //void delegates with no parameters
         static public Delegate Create(EventInfo evt, Action d)
         { 
             var handlerType = evt.EventHandlerType;
             var eventParams = handlerType.GetMethod("Invoke").GetParameters();
    
             //lambda: (object x0, EventArgs x1) => d()
             var parameters = eventParams.Select(p=>Expression.Parameter(p.ParameterType,"x"));
             var body = Expression.Call(Expression.Constant(d),d.GetType().GetMethod("Invoke"));
             var lambda = Expression.Lambda(body,parameters.ToArray());
             return Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke", false);
         }
    
         //void delegate with one parameter
         static public Delegate Create<T>(EventInfo evt, Action<T> d)
         {
             var handlerType = evt.EventHandlerType;
             var eventParams = handlerType.GetMethod("Invoke").GetParameters();
    
             //lambda: (object x0, ExampleEventArgs x1) => d(x1.IntArg)
             var parameters = eventParams.Select(p=>Expression.Parameter(p.ParameterType,"x")).ToArray();
             var arg    = getArgExpression(parameters[1], typeof(T));
             var body   = Expression.Call(Expression.Constant(d),d.GetType().GetMethod("Invoke"), arg);
             var lambda = Expression.Lambda(body,parameters);
             return Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke", false);
         }
    
         //returns an expression that represents an argument to be passed to the delegate
         static Expression getArgExpression(ParameterExpression eventArgs, Type handlerArgType)
         {
            if (eventArgs.Type==typeof(ExampleEventArgs) && handlerArgType==typeof(int))
            {
               //"x1.IntArg"
               var memberInfo = eventArgs.Type.GetMember("IntArg")[0];
               return Expression.MakeMemberAccess(eventArgs,memberInfo);
            }
    
            throw new NotSupportedException(eventArgs+"->"+handlerArgType);
         }
     }
    
    
     static class Test
     {
         public static void Main()
         { 
            var raiser  = new EventRaiser();
            var handler = new Handler();
    
            //void delegate with no parameters
            string eventName = "SomethingHappened";
            var eventinfo = raiser.GetType().GetEvent(eventName);
            eventinfo.AddEventHandler(raiser,EventProxy.Create(eventinfo,handler.HandleEvent));
    
            //void delegate with one parameter
            string eventName2 = "SomethingHappenedWithArg";
            var eventInfo2 = raiser.GetType().GetEvent(eventName2);
            eventInfo2.AddEventHandler(raiser,EventProxy.Create<int>(eventInfo2,handler.HandleEventWithArg));
    
            //or even just:
            eventinfo.AddEventHandler(raiser,EventProxy.Create(eventinfo,()=>Console.WriteLine("!")));  
            eventInfo2.AddEventHandler(raiser,EventProxy.Create<int>(eventInfo2,i=>Console.WriteLine(i+"!")));
    
            raiser.RaiseEvents();
         }
     }
    
    0 讨论(0)
提交回复
热议问题