From fbf8085fdcfecdb36ae613e5dd3c1102bd7ff36a Mon Sep 17 00:00:00 2001 From: rameel Date: Sun, 22 Nov 2026 19:24:06 +0500 Subject: [PATCH 1/2] binding: extract the base type check from method hiding HidesMethodsOf in MethodResolver combined two rules: finding base types by inheritance and letting a method of a class other than Object remove the methods of interfaces. Move the first rule to ReflectionExtensions.IsBaseTypeOf, so that only the rule specific to method invocation stays in MethodResolver and member lookup can use the same check. No behavior change. --- .../Internal/MethodResolver.cs | 15 ++++-------- .../Internal/ReflectionExtensions.cs | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs b/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs index 11b5cdb..11cd8c2 100644 --- a/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs +++ b/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs @@ -302,18 +302,11 @@ static bool IsHidden(Type type, Type[] declaringTypes) static bool HidesMethodsOf(Type other, Type type) { // - // The base types of an interface are its base interfaces and Object, while an interface - // is not a base type of a class or struct. As in C#, a method of a class other than Object - // also removes the methods declared in interfaces. Base types are found by inheritance: - // a variant interface can be assignable from another instantiation it does not inherit. + // As in C#, a method of a class other than Object also removes the methods declared + // in interfaces, although an interface is not a base type of a class. // - if (type.IsInterface) - return other.IsInterface - ? other.GetInterfaces().Contains(type) - : other.IsClass && other != typeof(object); - - return other.IsSubclassOf(type) - || other.IsInterface && type == typeof(object); + return type.IsBaseTypeOf(other) + || type.IsInterface && other.IsClass && other != typeof(object); } } diff --git a/src/Ramstack.ExpressionParser/Internal/ReflectionExtensions.cs b/src/Ramstack.ExpressionParser/Internal/ReflectionExtensions.cs index 6c6907e..ceb32f7 100644 --- a/src/Ramstack.ExpressionParser/Internal/ReflectionExtensions.cs +++ b/src/Ramstack.ExpressionParser/Internal/ReflectionExtensions.cs @@ -67,6 +67,30 @@ public static IEnumerable GetMemberLookupTypes(this Type type) return [type]; } + /// + /// Determines whether the specified type is a base type of another type. + /// + /// The type to evaluate. + /// The type whose base types are checked. + /// + /// if is a base type of ; + /// otherwise, . + /// + /// + /// As in C#, base types are determined by inheritance: the base types of a class or struct are its base classes, + /// and the base types of an interface are its base interfaces and . An interface implemented + /// by a class or struct is not its base type, and an instantiation of a variant interface is not a base type + /// of another instantiation that is assignable to it. A type is not a base type of itself. + /// + public static bool IsBaseTypeOf(this Type type, Type other) + { + if (type.IsInterface) + return other.IsInterface && other.GetInterfaces().Contains(type); + + return other.IsSubclassOf(type) + || other.IsInterface && type == typeof(object); + } + /// /// Determines whether the specified member is declared as static. /// From 85d2e0b9a3999f4197c9b010a72664bac5bd090c Mon Sep 17 00:00:00 2001 From: rameel Date: Sun, 22 Nov 2026 19:26:49 +0500 Subject: [PATCH 2/2] binding: report ambiguous members of variant base interfaces A field or property hid the members with the same name declared in any type assignable from its declaring type, and assignability takes variance into account. With a covariant IProducer declaring T Value and IBothProducers : IProducer, IProducer, IProducer is assignable from IProducer, so P.Value silently bound to IProducer.Value. C# reports an ambiguity (CS0229): neither instantiation is a base interface of the other. Use IsBaseTypeOf, which finds base types by inheritance, as for methods. It never treats a type as its own base type, so the separate check for the same declaring type is no longer needed. Classes were not affected, since they cannot be variant. --- .../DefaultBinder.cs | 4 +--- .../ExpressionParserTest.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Ramstack.ExpressionParser/DefaultBinder.cs b/src/Ramstack.ExpressionParser/DefaultBinder.cs index a0bb40d..5a6ec85 100644 --- a/src/Ramstack.ExpressionParser/DefaultBinder.cs +++ b/src/Ramstack.ExpressionParser/DefaultBinder.cs @@ -168,9 +168,7 @@ public void RegisterType(Type type, bool importAsStatic = false) // if (type is not null && candidates.Length > 1) candidates = candidates - .Where(m => !candidates.Any(c => - c.DeclaringType != m.DeclaringType - && m.DeclaringType!.IsAssignableFrom(c.DeclaringType))) + .Where(m => !candidates.Any(c => m.DeclaringType!.IsBaseTypeOf(c.DeclaringType!))) .ToArray(); switch (candidates.Length) diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 9206d73..59c60a1 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -389,6 +389,16 @@ public void Parse_MemberOfUnrelatedBaseInterfaces_ReturnsAmbiguityError(string e Assert.That(result.ErrorMessage, Does.StartWith("Ambiguous match found")); } + [Test] + public void Parse_MemberOfVariantBaseInterfaces_ReturnsAmbiguityError() + { + var parameter = Expression.Parameter(typeof(InterfaceContext), "c"); + var result = ExpressionParser.Parse("Producers.Value", new ContextAwareBinder(parameter)); + + Assert.That(result.Success, Is.False); + Assert.That(result.ErrorMessage, Does.StartWith("Ambiguous match found")); + } + [TestCaseSource(nameof(InconvertibleResultSource))] public void Evaluate_ResultNotConvertibleToResultType_ThrowsArgumentException(Func evaluate, string expected) { @@ -1096,6 +1106,17 @@ public class BothConsumers : IBothConsumers string IConsumer.Pick(string value) => "IConsumer.Pick(string)"; } + public interface IProducer + { + T Value { get; } + } + + // + // IProducer is assignable from IProducer by covariance, + // but neither is a base interface of the other. + // + public interface IBothProducers : IProducer, IProducer; + public class InterfaceImplementation : IDerivedInterface { int IBaseInterface.Value => 1; @@ -1116,6 +1137,7 @@ public class InterfaceContext public IDerivedInterface Derived { get; } = new InterfaceImplementation(); public IListAndReadOnlyList? Both { get; } public IBothConsumers Consumers { get; } = new BothConsumers(); + public IBothProducers? Producers { get; } } private class BaseMembers