浏览代码

Prefer CLR type's shadowing property over base's hidden (#1963)

mspertus 10 月之前
父节点
当前提交
ca8a653428
共有 2 个文件被更改,包括 30 次插入1 次删除
  1. 22 1
      Jint.Tests/Runtime/InteropTests.MemberAccess.cs
  2. 8 0
      Jint/Runtime/Interop/TypeResolver.cs

+ 22 - 1
Jint.Tests/Runtime/InteropTests.MemberAccess.cs

@@ -280,4 +280,25 @@ public partial class InteropTests
 
         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 - 0
Jint/Runtime/Interop/TypeResolver.cs

@@ -317,6 +317,14 @@ public sealed class TypeResolver
                     {
                         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;
                         }