dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
2.01k stars 379 forks source link

Exception "Incorrect number of arguments supplied" when params array has null in first parameter #263

Closed stefanoIT closed 1 year ago

stefanoIT commented 1 year ago

Hello Davide and everyone.

Consider this function

public static class ParamArrayTester
{
  public static int ParamArrayObjects(params object[] values)
  {
    return values.Length;
  }
}

If I implement the following test:

[Test]
public void Test_ParamArrayObjects()
{
  Interpreter dexp = new Interpreter();

  dexp.Reference(typeof(ParamArrayTester));
  string expText = "ParamArrayTester.ParamArrayObjects(null, null, 0, 0)";
  Object resultObject = dexp.Eval(expText);

  Assert.AreEqual(4, (int)resultObject);
}

Exception :

System.ArgumentException: 'Incorrect number of arguments supplied for call to method 'Int32 ParamArrayObjects(System.Object[])' (Parameter 'method')'

On Stack:

ParseNormalMethodInvocation return Expression.Call(instance, (MethodInfo)method.MethodBase, method.PromotedParameters);

The following call works perfectly :

string expText = "ParamArrayTester.ParamArrayObjects(0, null, null, null)";

So the guess is that the first parameter makes the difference somehow.

Thanks in advance, cheers.

davideicardi commented 1 year ago

Yes, it looks like a bug somewhere in the params discovery. I will try to investigate, but as usual any help is appreciated ...