Skip to content

Commit

Permalink
Prefer CLR type's shadowing property over base's hidden (#1963)
Browse files Browse the repository at this point in the history
  • Loading branch information
mspertus authored Sep 4, 2024
1 parent 3c4af55 commit ca8a653
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Jint.Tests/Runtime/InteropTests.MemberAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,25 @@ public void CanConfigureStrictAccess()

engine.Invoking(e => e.Evaluate("obj.AgeMissing")).Should().Throw<MissingMemberException>();
}
}

public class ClassWithPropertyToHide
{
public int x { get; set; } = 2;
public int y { get; set; } = 3;
}

public class ClassThatHidesProperty : ClassWithPropertyToHide
{
public new bool x { get; set; } = true;
}

[Fact]
public void ShouldRespectExplicitHiding()
{
var engine = new Engine();

engine.SetValue("obj", new ClassThatHidesProperty());
engine.Evaluate("obj.x").AsBoolean().Should().BeTrue();
engine.Evaluate("obj.y").AsNumber().Should().Be(3);
}
}
8 changes: 8 additions & 0 deletions Jint/Runtime/Interop/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ internal bool TryFindMemberAccessor(
{
if (memberNameComparer.Equals(name, memberName))
{
// If one property hides another (e.g., by public new), the derived property is returned.
if (property is not null
&& p.DeclaringType is not null
&& property.DeclaringType is not null
&& property.DeclaringType.IsSubclassOf(p.DeclaringType))
{
continue;
}
property = p;
break;
}
Expand Down

0 comments on commit ca8a653

Please sign in to comment.