2
0
Эх сурвалжийг харах

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

mspertus 10 сар өмнө
parent
commit
ca8a653428

+ 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>();
         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 (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;
                             property = p;
                             break;
                             break;
                         }
                         }