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
7 changes: 4 additions & 3 deletions src/Ramstack.ExpressionParser/DefaultBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ public void RegisterType(Type type, bool importAsStatic = false)
.ToArray();

//
// The lookup in an interface includes its base interfaces, so, as in C#, a member
// of a derived interface hides the members with the same name of its base interfaces.
// As in C#, a field or property of a derived type hides matching members of its
// base types. For static imports, keep matches from each imported type so that
// a name found in two imports is ambiguous, as with using static in C#.
//
if (type?.IsInterface == true && candidates.Length > 1)
if (type is not null && candidates.Length > 1)
candidates = candidates
.Where(m => !candidates.Any(c =>
c.DeclaringType != m.DeclaringType
Expand Down
37 changes: 37 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ public void Evaluate_MemberOfBaseInterfaceOrObject_BindsMember(string expression
public void Evaluate_MemberHiddenByDerivedInterface_BindsMemberOfDerivedInterface(string expression, string expected) =>
Assert.That(ExpressionParser.Evaluate(expression, new InterfaceContext()), Is.EqualTo(expected));

[TestCase("Derived.Field", 2)]
[TestCase("Derived.Property", "derived")]
public void Evaluate_MemberHiddenByDerivedClass_BindsMemberOfDerivedClass(string expression, object expected) =>
Assert.That(ExpressionParser.Evaluate(expression, new { Derived = new DerivedMembers() }), Is.TypeOf(expected.GetType()).And.EqualTo(expected));

[Test]
public void Evaluate_UnqualifiedMemberInDerivedContext_BindsMemberOfDerivedClass() =>
Assert.That(ExpressionParser.Evaluate("Field", new DerivedMembers()), Is.TypeOf<int>().And.EqualTo(2));

[Test]
public void Parse_SameMemberImportedFromBaseAndDerivedTypes_ReturnsAmbiguityError()
{
var binder = new DefaultBinder();
binder.RegisterType(typeof(BaseMembers), importAsStatic: true);
binder.RegisterType(typeof(DerivedMembers), importAsStatic: true);

var result = ExpressionParser.Parse("ImportedMember", binder);

Assert.That(result.Success, Is.False);
Assert.That(result.ErrorMessage, Does.StartWith("Ambiguous match found")
.And.Contain(nameof(BaseMembers)).And.Contain(nameof(DerivedMembers)));
}

[TestCase("IDerivedInterface.Create()", "IBaseInterface.Create")]
[TestCase("IDerivedInterface.ReferenceEquals(Derived, Derived)", true)]
public void Evaluate_StaticMemberOfBaseInterfaceOrObject_BindsMember(string expression, object expected)
Expand Down Expand Up @@ -967,6 +990,20 @@ public class InterfaceContext
public IListAndReadOnlyList? Both { get; }
}

private class BaseMembers
{
public static int ImportedMember => 1;
public readonly int Field = 1;
public int Property { get; } = 1;
}

private sealed class DerivedMembers : BaseMembers
{
public static new int ImportedMember => 2;
public new readonly int Field = 2;
public new string Property { get; } = "derived";
}

public class LogicalNotContext
{
public bool Flag { get; } = true;
Expand Down