Răsfoiți Sursa

Fixes duplicate class property registration (#19558)

* Fix ClassBindingManager property dictionary insert

* Prepend ClassPropertyPrefix to key when inserting into registered properties dictionary to be consistent with lookup.

* Added ClassBindingManager unit tests
Colton 1 lună în urmă
părinte
comite
934c0f8349

+ 7 - 4
src/Avalonia.Base/ClassBindingManager.cs

@@ -33,10 +33,13 @@ namespace Avalonia
         }
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public static AvaloniaProperty GetClassProperty(string className) =>
-            s_RegisteredProperties.TryGetValue(ClassPropertyPrefix + className, out var property)
-                ? property 
-                : s_RegisteredProperties[className] = RegisterClassProxyProperty(className);
+        public static AvaloniaProperty GetClassProperty(string className)
+        {
+            var prefixedClassName = ClassPropertyPrefix + className;
+            return s_RegisteredProperties.TryGetValue(prefixedClassName, out var property)
+                ? property
+                : s_RegisteredProperties[prefixedClassName] = RegisterClassProxyProperty(className);
+        }
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public static bool IsClassesBindingProperty(AvaloniaProperty property, [NotNullWhen(true)] out string? classPropertyName)

+ 24 - 0
tests/Avalonia.Base.UnitTests/ClassBindingManagerTests.cs

@@ -0,0 +1,24 @@
+using Xunit;
+
+namespace Avalonia.Base.UnitTests
+{
+    public class ClassBindingManagerTests
+    {
+
+        [Fact]
+        public void GetClassProperty_Should_Return_Same_Instance_For_Same_Class()
+        {
+            var property1 = ClassBindingManager.GetClassProperty("Foo");
+            var property2 = ClassBindingManager.GetClassProperty("Foo");
+            Assert.Same(property1, property2);
+        }
+
+        [Fact]
+        public void GetClassProperty_Should_Return_Different_Instances_For_Different_Classes()
+        {
+            var property1 = ClassBindingManager.GetClassProperty("Foo");
+            var property2 = ClassBindingManager.GetClassProperty("Bar");
+            Assert.NotSame(property1, property2);
+        }
+    }
+}