Browse Source

Merge pull request #2270 from mstr2/property-hotfix

Fixes a bug in AvaloniaPropertyRegistry
Steven Kirk 6 years ago
parent
commit
8e0c411616

+ 7 - 3
src/Avalonia.Base/AvaloniaPropertyRegistry.cs

@@ -13,7 +13,7 @@ namespace Avalonia
     /// </summary>
     public class AvaloniaPropertyRegistry
     {
-        private readonly IList<AvaloniaProperty> _properties =
+        private readonly List<AvaloniaProperty> _properties =
             new List<AvaloniaProperty>();
         private readonly Dictionary<Type, Dictionary<int, AvaloniaProperty>> _registered =
             new Dictionary<Type, Dictionary<int, AvaloniaProperty>>();
@@ -30,6 +30,11 @@ namespace Avalonia
         public static AvaloniaPropertyRegistry Instance { get; }
             = new AvaloniaPropertyRegistry();
 
+        /// <summary>
+        /// Gets a list of all registered properties.
+        /// </summary>
+        internal IReadOnlyList<AvaloniaProperty> Properties => _properties;
+
         /// <summary>
         /// Gets all non-attached <see cref="AvaloniaProperty"/>s registered on a type.
         /// </summary>
@@ -250,8 +255,7 @@ namespace Avalonia
             {
                 inner.Add(property.Id, property);
             }
-
-            _properties.Add(property);
+            
             _attachedCache.Clear();
         }
     }

+ 12 - 0
tests/Avalonia.Base.UnitTests/AvaloniaPropertyRegistryTests.cs

@@ -19,6 +19,18 @@ namespace Avalonia.Base.UnitTests
             p = AttachedOwner.AttachedProperty;
         }
 
+        [Fact]
+        public void Registered_Properties_Count_Reflects_Newly_Added_Attached_Property()
+        {
+            var registry = new AvaloniaPropertyRegistry();
+            var metadata = new StyledPropertyMetadata<int>();
+            var property = new AttachedProperty<int>("test", typeof(object), metadata, true);
+            registry.Register(typeof(object), property);
+            registry.RegisterAttached(typeof(AvaloniaPropertyRegistryTests), property);
+
+            Assert.Equal(1, registry.Properties.Count);
+        }
+
         [Fact]
         public void GetRegistered_Returns_Registered_Properties()
         {