C # - delegate with any number of configurable options

I need a delegate that I can save in a variable for later use, which has a custom number of custom parameters. I mean, I want to use it in different ways with different types of returned data and different arguments. For instance:

public double Sum (double a, double b) {return a + b;}
public char GetFirst (string a) {return a[0];}
public bool canFlipTable (object[] thingsOnIt) {return thingsOnIt.Length <= 3;}

DoTheThing<double> thing1 = new DoTheThing<double>(Sum);
DoTheThing<char> thing2 = new DoTheThing<char>(GetFirst);
DoTheThing<bool> thing3 = new DoTheThing<bool>(canFlipTable);

thing1.Call(10.3, 5.6);  //15.9
thing2.Call("Hello World");  //'H'
thing3.Call(new object[] {new Lamp(), new Laptop(), new CoffeMug()});  //true

I already figured out the return value and the calling method, but I had a problem storing the methods

If I use the “public DoTheThing (action method)”, it says the arguments are not the same I even tried with a delegate that had “params object [] p” as the arguments, but it didn’t work either

EDIT: I forgot to say that the method will always have a return type and at least 1 parameter

2: - -, , , . , , , , , , , .

sofar:

public class DoTheThing <T>
{
    public delegate T Method(params object[] parameters);

    Func<T> method;
    ParameterInfo[] pInfo;

    public DoTheThing (Method method)
    {
        this.method = method;
        Type type = typeof(Method);
        MethodInfo info = type.GetMethod ("Invoke");
        if (info.ReturnType != typeof(T)) {
            throw new Exception ("Type of DoTheThing and method don't match");
        }
        pInfo = info.GetParameters ();
    }

    public T Call (params object[] parameters) {
        if (parameters.Length != pInfo.Length) {
            throw new Exception ("Wrong number of arguments, " + parameters.Length + " instead of " + pInfo.Length);
            return default(T);
        }

        for (int i = 0; i < parameters.Length; i++) {
            if (pInfo[i].ParameterType != parameters[i].GetType()) {
                throw new Exception ("Wrong parameter: " + parameters [i].GetType () + " instead of " + pInfo [i].ParameterType + " at position: " + i);
                return default(T);
            }
        }

        return (T)method.DynamicInvoke (parameters);
    }
}
0
1

, , , . , , , .

, , MulticastDelegate. , , MulticastDelegate . , :

    public static double Sum(double a, double b)
    {
        return a + b;
    }

    public static string SayHello()
    {
        return "Hello";
    }

    static void Main(string[] args)
    {
        MulticastDelegate mydel = new Func<double, double, double>(Sum);
        var ret = mydel.DynamicInvoke(1, 2);
        System.Console.WriteLine(ret);

        mydel = new Func<string>(SayHello);
        ret = mydel.DynamicInvoke();
        System.Console.WriteLine(ret);


        mydel = new Func<string, int, string> ((s, i) => { 
            return $"Would be {s}, {i} times";
         });
        ret = mydel.DynamicInvoke("Hello", 5);
        System.Console.WriteLine(ret);
    }

"mydel" (MulticastDelegate), . , .

+1

All Articles