From 4cbb4c7b5d7816264e4f754612ab5865589db638 Mon Sep 17 00:00:00 2001 From: denbell5 Date: Tue, 9 Apr 2024 16:43:34 +0300 Subject: [PATCH] Fix Issue #305 --- src/DynamicExpresso.Core/Parsing/Parser.cs | 18 ++++++++++++- test/DynamicExpresso.UnitTest/GithubIssues.cs | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/DynamicExpresso.Core/Parsing/Parser.cs b/src/DynamicExpresso.Core/Parsing/Parser.cs index 08e5985..b606b96 100644 --- a/src/DynamicExpresso.Core/Parsing/Parser.cs +++ b/src/DynamicExpresso.Core/Parsing/Parser.cs @@ -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) @@ -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; } diff --git a/test/DynamicExpresso.UnitTest/GithubIssues.cs b/test/DynamicExpresso.UnitTest/GithubIssues.cs index 95d39d6..010292e 100644 --- a/test/DynamicExpresso.UnitTest/GithubIssues.cs +++ b/test/DynamicExpresso.UnitTest/GithubIssues.cs @@ -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