dimanche 26 février 2017

Set Class Member to Function

I have a class that interacts with another class solely through reflection. This class that I am interacting with has some delegates and I am trying to set them. Heres what they look like:

public class ClassThatIAmReflecting {
    public delegate void OnSuccessDelegate(bool value);

    public static OnSuccessDelegate OnSuccess;
} 

And here is what I am trying:

public class MyClass {

    void Init() {
        Type type = Type.GetType("ClassThatIAmReflecting");

        FieldInfo fieldInfo = type.GetField("OnSuccess", BindingFlags.Public | BindingFlags.Static);
        fieldInfo.SetValue(null, HandleOnSuccess);
    }

    void HandleOnSuccess(bool value) {
        // do stuff ...
    }
}

The error I am getting is that it cannot convert the action to a object. Any help on how I can do this?





Aucun commentaire:

Enregistrer un commentaire