vendredi 12 avril 2019

CreateDelegate using one general method

I want to create various type of delegates with just one general method like this.

public static object Boo(params object[] args) {
    return null;
}

MethodInfo boo = /* MethodInfo of Boo */

// Boo's arg.Length should be `0`
Action action = (Action)Delegate.CreateDelegate(typeof(Action), null, boo);

Func<int> action2 = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), null, boo);
Func<string, string> action3 = (Func<string, string>)Delegate.CreateDelegate(typeof(Func<string, string>), null, boo);

So, If you invoke action3 just like this

var ret = action3.Invoke("booo");

Boo's args.Length should be 1 and ret is null.

Is it possible in C#?

  • No dynamic, generic or linq.exprssion sry.




Aucun commentaire:

Enregistrer un commentaire