Browse Source

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
pull/19583/head
Colton 5 months ago
committed by GitHub
parent
commit
934c0f8349
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      src/Avalonia.Base/ClassBindingManager.cs
  2. 24
      tests/Avalonia.Base.UnitTests/ClassBindingManagerTests.cs

11
src/Avalonia.Base/ClassBindingManager.cs

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

24
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);
}
}
}
Loading…
Cancel
Save