dimanche 19 février 2017

DynamicInvoke Number of parameter does not match expected count

I am trying to wrap a lambda inside of Func<..> assigned to dynamic and call it dynamically but it throws an exception about parameter count.

MCVE

using System;

public class Test
{
    public static void Main()
    {

        // your code goes here
        try {
            dynamic plus = new Func<long, long, long>((a, b) => a + b);
            Type functionType = plus.GetType();
            int arity = functionType.GetGenericArguments().Length - 1;
            Console.WriteLine("Arity {0}", arity);
            long[] parameters = new long[arity];

            long result = plus.DynamicInvoke(parameters);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
} 

Exception: Number of parameter does not match expected count.





Aucun commentaire:

Enregistrer un commentaire