vendredi 25 janvier 2019

Incorrect number of arguments supplied for call to method C#

I have a method which takes 2 parameters. I want to build a method in runtime, which will call this method and pass one parameter by default. Another parameter will be passed in new function.

I tried to create lambda expression which calls this method but i've got an error: Incorrect number of arguments supplied for call to method.

static class Program
{
    static void Main(string[] args)
    {
        var method = typeof(Program).GetMethod("Method");
        // here i want to set first parameter as "parameter1" when new method will be called
        var lambda = Expression.Lambda<Func<string, string>>(Expression.Call(method, Expression.Constant("parameter1")));
        var adapter = lambda.Compile();
        // and here i wanna pass only one agrument - second (parameter2)
        var result = adapter("parameter2");

        // expected result "parameter1 ---- parameter2"
    }

    public static string Method(string parameter1, string parameter2)
    {
        return $"{parameter1} ---- {parameter2}";
    }

I wanna pass only second parameter when function will be called. First must be specified automatically.





Aucun commentaire:

Enregistrer un commentaire