Skip to content
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
4 changes: 1 addition & 3 deletions src/Ramstack.ExpressionParser/DefaultBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 4 additions & 11 deletions src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/Ramstack.ExpressionParser/Internal/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ public static IEnumerable<Type> GetMemberLookupTypes(this Type type)
return [type];
}

/// <summary>
/// Determines whether the specified type is a base type of another type.
/// </summary>
/// <param name="type">The type to evaluate.</param>
/// <param name="other">The type whose base types are checked.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="type" /> is a base type of <paramref name="other" />;
/// otherwise, <see langword="false" />.
/// </returns>
/// <remarks>
/// 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 <see cref="object" />. 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.
/// </remarks>
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);
}

/// <summary>
/// Determines whether the specified member is declared as static.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?> evaluate, string expected)
{
Expand Down Expand Up @@ -1096,6 +1106,17 @@ public class BothConsumers : IBothConsumers
string IConsumer<string>.Pick(string value) => "IConsumer<string>.Pick(string)";
}

public interface IProducer<out T>
{
T Value { get; }
}

//
// IProducer<object> is assignable from IProducer<string> by covariance,
// but neither is a base interface of the other.
//
public interface IBothProducers : IProducer<object>, IProducer<string>;

public class InterfaceImplementation : IDerivedInterface
{
int IBaseInterface.Value => 1;
Expand All @@ -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
Expand Down