mercredi 12 août 2015

Get delegate handler from event [duplicate]

I am trying to get the event handler without doing any changes in MyControl class. I tried to use reflection but I am not able to get the handler. Following is a code sample. Thanks

public class MyControl : Control
{
    public void Register()
    {
        SizeChanged += MyControl_SizeChanged;
    }

    void MyControl_SizeChanged(object sender, EventArgs e)
    {
        // Do something
    }
}

//[TestFixture]
public class MyControlTest
{
    //  [Test]
    public void RegisterTest()
    {

        var control = new MyControl();
        control.Register();

        var eventInfo = control.GetType().GetEvent("SizeChanged", BindingFlags.Public | BindingFlags.Instance);

        // Need to get the handler (delegate) and GetInvocationList().Count
        EventHandler handler = ...;

        var count = handler.GetInvocationList().Count();
        Assert.That(count, IsolationLevel.EqualTo(1));
    }
}





Aucun commentaire:

Enregistrer un commentaire