C# Dynamic Event Subscription

前端 未结 9 1858
無奈伤痛
無奈伤痛 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:24

    Try LinFu--it has a universal event handler that lets you bind to any event at runtime. For example, here's you you can bind a handler to the Click event of a dynamic button:

    // Note: The CustomDelegate signature is defined as:
    // public delegate object CustomDelegate(params object[] args);
    CustomDelegate handler = delegate
                             {
                               Console.WriteLine("Button Clicked!");
                               return null;
                             };
    
    Button myButton = new Button();
    // Connect the handler to the event
    EventBinder.BindToEvent("Click", myButton, handler);
    

    LinFu lets you bind your handlers to any event, regardless of the delegate signature. Enjoy!

    You can find it here: http://www.codeproject.com/KB/cs/LinFuPart3.aspx

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

    It is possible to subscribe to an event using Reflection

    var o = new SomeObjectWithEvent;
    o.GetType().GetEvent("SomeEvent").AddEventHandler(...);
    

    http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx

    Now here is going to be the problem that you are going to have to solve. The delegates required for each event handler will have different signatures. You are going to have to find away to create these methods dynamically, which probably means Reflection.Emit, or you are going to have to limit your self to a certain delegate so that you can handle it with compiled code.

    Hope this helps.

    0 讨论(0)
  • 2020-12-01 03:29
    public TestForm()
    {
        Button b = new Button();
    
        this.Controls.Add(b);
    
        MethodInfo method = typeof(TestForm).GetMethod("Clickbutton",
        BindingFlags.NonPublic | BindingFlags.Instance);
        Type type = typeof(EventHandler);
    
        Delegate handler = Delegate.CreateDelegate(type, this, method);
    
        EventInfo eventInfo = cbo.GetType().GetEvent("Click");
    
        eventInfo.AddEventHandler(b, handler);
    
    }
    
    void Clickbutton(object sender, System.EventArgs e)
    {
        // Code here
    }
    
    0 讨论(0)
  • 2020-12-01 03:29

    I recently wrote a series of blog posts describing unit testing events, and one of the techniques I discuss describes dynamic event subscription. I used reflection and MSIL (code emitting) for the dynamic aspects, but this is all wrapped up nicely. Using the DynamicEvent class, events can be subscribed to dynamically like so:

    EventPublisher publisher = new EventPublisher();
    
    foreach (EventInfo eventInfo in publisher.GetType().GetEvents())
    {
        DynamicEvent.Subscribe(eventInfo, publisher, (sender, e, eventName) =>
        {
            Console.WriteLine("Event raised: " + eventName);
        });
    }
    

    One of the features of the pattern I implemented was that it injects the event name into the call to the event handler so you know which event has been raised. Very useful for unit testing.

    The blog article is quite lengthy as it is describing an event unit testing technique, but full source code and tests are provided, and a detailed description of how dynamic event subscription was implemented is detailed in the last post.

    http://gojisoft.com/blog/2010/04/22/event-sequence-unit-testing-part-1/

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

    It's not a completely general solution, but if all your events are of the form void Foo(object o, T args) , where T derives from EventArgs, then you can use delegate contravariance to get away with it. Like this (where the signature of KeyDown is not the same as that of Click) :

        public Form1()
        {
            Button b = new Button();
            TextBox tb = new TextBox();
    
            this.Controls.Add(b);
            this.Controls.Add(tb);
            WireUp(b, "Click", "Clickbutton");
            WireUp(tb, "KeyDown", "Clickbutton");
        }
    
        void WireUp(object o, string eventname, string methodname)
        {
            EventInfo ei = o.GetType().GetEvent(eventname);
    
            MethodInfo mi = this.GetType().GetMethod(methodname, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
    
            Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, this, mi);
    
            ei.AddEventHandler(o, del);
    
        }
        void Clickbutton(object sender, System.EventArgs e)
        {
            MessageBox.Show("hello!");
        }
    
    0 讨论(0)
  • 2020-12-01 03:39

    What you want can be achieved using dependency injection. For example Microsoft Composite UI app block does exactly what you described

    0 讨论(0)
提交回复
热议问题