Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect overload resolution #215

Merged
merged 2 commits into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2068,8 +2068,13 @@ private static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Exp
{
if (x.HasDefaultValue)
return Expression.Constant(x.DefaultValue, x.ParameterType);

if (HasParamsArrayType(x))
{
method.HasParamsArray = true;
return Expression.NewArrayInit(x.ParameterType.GetElementType());
}

throw new Exception("No default value found!");
}));

Expand Down Expand Up @@ -2437,7 +2442,7 @@ private static bool MethodHasPriority(Expression[] args, MethodData method, Meth
return true;

// if a method has a params parameter, it can have less parameters than the number of arguments
if (method.Parameters.Length > otherMethod.Parameters.Length)
if (method.HasParamsArray && otherMethod.HasParamsArray && method.Parameters.Length > otherMethod.Parameters.Length)
return true;

return better;
Expand Down
22 changes: 22 additions & 0 deletions test/DynamicExpresso.UnitTest/MemberInvocationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ internal static class Utils
public static int WithParamsArray(int i, params int[] j) => 4;
public static int WithParamsArray(int i, int j) => 5;

public static int WithParamsArray2(string str, Exception e) => 6;
public static int WithParamsArray2(string str, Exception e, params string[] args) => 7;
public static int WithParamsArray2(string str, Exception e, params int[] args) => 8;


public static List<T> Array<T>(params T[] array)
{
return new List<T>(array);
Expand Down Expand Up @@ -480,6 +485,23 @@ public void Method_overload_params_array()
Assert.AreEqual(4, target.Eval("Utils.WithParamsArray(1, 2, 3, 4)"));
}

[Test]
public void Method_overload_params_array_2()
{
var target = new Interpreter();
target.Reference(typeof(Utils));

var str = "str";
var e = new Exception();
var intg = 4;
target.SetVariable("str", str);
target.SetVariable("e", e);
target.SetVariable("intg", intg);
Assert.AreEqual(6, target.Eval("Utils.WithParamsArray2(str, e)"));
Assert.AreEqual(7, target.Eval("Utils.WithParamsArray2(str, e, str, str)"));
Assert.AreEqual(8, target.Eval("Utils.WithParamsArray2(str, e, intg, intg)"));
}

private interface MyTestInterface
{
}
Expand Down