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/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. /// 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