mardi 22 septembre 2015

Call a method passed as parameter on an object passed as parameter

I have a class defining several functions with the same signature

public class MyClass
{
    public string MyParam1() ...
    public string MyParam2() ...
    ...
}

From another class I want to call a method passed as parameter on an object passed as parameter

void MyFunction(MyClass anObject, Func<string> aMethod)
{
    string val = ??? // Here call aMethod on anObject
}

I'm pretty sure it's possible to do this using reflection, but is there a not to difficult way to do it ?

In fact I have a set of objects, not a single object, it's why I cannot call directly the method of the object.





Aucun commentaire:

Enregistrer un commentaire