Skip to content

Commit

Permalink
Merge pull request #105 from weltall/master
Browse files Browse the repository at this point in the history
Handle default parameters type explicitly
  • Loading branch information
davideicardi committed Feb 3, 2019
2 parents 058d6d6 + 6d4b7ae commit 273e373
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ private static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Exp
}

// Add default params, if needed.
promotedArgs.AddRange(method.Parameters.Skip(promotedArgs.Count).Select(x => Expression.Constant(x.DefaultValue)));
promotedArgs.AddRange(method.Parameters.Skip(promotedArgs.Count).Select(x => Expression.Constant(x.DefaultValue, x.ParameterType)));

method.PromotedParameters = promotedArgs.ToArray();

Expand Down
23 changes: 23 additions & 0 deletions test/DynamicExpresso.UnitTest/MemberInvocationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,24 @@ public void Method_with_optional_param()
Assert.AreEqual(x.MethodWithOptionalParam(w, y, z), target.Eval("x.MethodWithOptionalParam(w, y, z)", parameters));
}

[Test]
public void Method_with_optional_null_param()
{
var target = new Interpreter();

var x = new MyTestService();
var y = "1";
var z = "2";
var parameters = new[] {
new Parameter("x", x.GetType(), x),
new Parameter("y", y.GetType(), y),
new Parameter("z", z.GetType(), z),
};

Assert.AreEqual(x.MethodWithOptionalNullParam(y), target.Eval("x.MethodWithOptionalNullParam(y)", parameters));
Assert.AreEqual(x.MethodWithOptionalNullParam(y, z), target.Eval("x.MethodWithOptionalNullParam(y, z)", parameters));
}

private interface MyTestInterface
{
}
Expand Down Expand Up @@ -456,6 +474,11 @@ public string MethodWithOptionalParam(string param1, string param2 = "2", string
return string.Format("{0} {1} {2}", param1, param2, param3);
}

public string MethodWithOptionalNullParam(string param1, string param2 = null)
{
return string.Format("{0} {1}", param1, param2 ?? "(null)");
}

public DateTime this[int i]
{
get { return AField.AddDays(i); }
Expand Down

0 comments on commit 273e373

Please sign in to comment.