Skip to content

Commit

Permalink
fix: Issue #305 (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
denbell5 authored Apr 10, 2024
1 parent 2696232 commit f966f84
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2711,7 +2711,10 @@ private static bool MethodHasPriority(Expression[] args, MethodData method, Meth
if (better)
return true;

if (!method.MethodBase.IsGenericMethod && otherMethod.MethodBase.IsGenericMethod)
if (method.MethodBase != null &&
otherMethod.MethodBase != null &&
!method.MethodBase.IsGenericMethod &&
otherMethod.MethodBase.IsGenericMethod)
return true;

if (!method.HasParamsArray && otherMethod.HasParamsArray)
Expand All @@ -2721,6 +2724,19 @@ private static bool MethodHasPriority(Expression[] args, MethodData method, Meth
if (method.HasParamsArray && otherMethod.HasParamsArray && method.Parameters.Length > otherMethod.Parameters.Length)
return true;

if (method is IndexerData indexer && otherMethod is IndexerData otherIndexer)
{
var declaringType = indexer.Indexer.DeclaringType;
var otherDeclaringType = otherIndexer.Indexer.DeclaringType;

var isOtherIndexerIsInParentType = otherDeclaringType.IsAssignableFrom(declaringType);
if (isOtherIndexerIsInParentType)
{
var isIndexerIsInDescendantType = !declaringType.IsAssignableFrom(otherDeclaringType);
return isIndexerIsInDescendantType;
}
}

return better;
}

Expand Down
26 changes: 26 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,32 @@ public void GitHub_Issue_295() {
var doesntWork = (string) evaluator.Eval("StringConcat(GlobalSettings.MyTestPath,\"test.txt\")");
Assert.That(doesntWork, Is.EqualTo("C:\\delme\\test.txt"));
}

#region GitHub_Issue_305

public class _305_A
{
public string this[int index] => "some string";
}

public class _305_B : _305_A
{
public new int this[int index] => 25;
}

[Test]
public void GitHub_Issue_305()
{
var b = new _305_B();

var interpreter = new Interpreter();
var lambda = interpreter.Parse("this[0]", new Parameter("this", b));
var res = lambda.Invoke(b);

Assert.AreEqual(25, res);
}

#endregion
}

internal static class GithubIssuesTestExtensionsMethods
Expand Down

0 comments on commit f966f84

Please sign in to comment.