lundi 23 novembre 2015

C# Reflection on loaded assembly and subscribe to it's event

I'm trying to figure out why this code does not raise a custom event when subscribing to loaded assembly's events by reflection:

public interface ISignalR
{        
    event EventHandler<SignalR_EventArgs> ProgressChanged; 
}    
 public class Test : ISignalR
{               
    public event EventHandler<SignalR_EventArgs> ProgressChanged = delegate { };

    protected virtual void OnProgressChanged(SignalR_EventArgs e)
    {            
        EventHandler<SignalR_EventArgs> handler = ProgressChanged;            
        if (handler != null)
        {                
            handler(this, e);
        }
    }


    static Test()
    {


    }

    public string CallMe(string output)
    {

        OnProgressChanged(new SignalR_EventArgs { MethodName = "MethodName", HubConnectionId = "", Response = "This is A response" });          


        return "yay";
    }
}

And For my reflection:

MethodInfo mi = this.GetType().GetMethod("OnProgressChanged", BindingFlags.Instance |
                                                                                  BindingFlags.Static |
                                                                                  BindingFlags.Public |
                                                                                  BindingFlags.FlattenHierarchy);

                    EventInfo eInfo = type.GetEvent("ProgressChanged", BindingFlags.Instance |
                                                                       BindingFlags.Static |
                                                                       BindingFlags.Public |
                                                                       BindingFlags.FlattenHierarchy);


                    Type eInfoHandlerType = eInfo.EventHandlerType;



                    Delegate del = Delegate.CreateDelegate(eInfoHandlerType, this, mi);
                    eInfo.AddEventHandler(Activator.CreateInstance(type), del);


                    var x = eInfo.GetAddMethod();


public void OnProgressChanged(object sender, SignalR_EventArgs e)
    {

    }

    public void add_ProgressChanged(EventHandler<WGM.SignalR.Common.SignalR_EventArgs> e)
    {

    }

But none of the OnProgressChanged \ add_ProgressChanged seem to be raised when the assembly's event (ProgressChanged is raised).





Aucun commentaire:

Enregistrer un commentaire