From ace9039e2631c105c46a87fefb71f7a607412720 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 May 2026 16:54:05 +0200 Subject: [PATCH] Remove dead code in AvaloniaObject. (#21452) No implementation of `BindingBase` implements `IObservable` any more, so the `TryBindStyledPropertyUntyped` and `TryBindDirectPropertyUntyped` methods always returned null. Remove them, and move the `IsReadOnly` check into a separate method in the spirit of extracting non-generic code from frequently used generic types. --- src/Avalonia.Base/AvaloniaObject.cs | 78 ++++++----------------------- 1 file changed, 14 insertions(+), 64 deletions(-) diff --git a/src/Avalonia.Base/AvaloniaObject.cs b/src/Avalonia.Base/AvaloniaObject.cs index 3ad488b615..60ffc71c98 100644 --- a/src/Avalonia.Base/AvaloniaObject.cs +++ b/src/Avalonia.Base/AvaloniaObject.cs @@ -463,39 +463,12 @@ namespace Avalonia IObservable source, BindingPriority priority = BindingPriority.LocalValue) { - return TryBindStyledPropertyUntyped(property, source, priority) - ?? _values.AddBinding(property, source, priority); - } - - // Non-generic path extracted to avoid unnecessary generic code duplication - private BindingExpressionBase? TryBindStyledPropertyUntyped( - AvaloniaProperty property, - IObservable source, - BindingPriority priority) - { - Debug.Assert(!property.IsDirect); ThrowHelper.ThrowIfNull(property, nameof(property)); ThrowHelper.ThrowIfNull(source, nameof(source)); VerifyAccess(); ValidatePriority(priority); - if (source is BindingBase b) - { - if (b.CreateInstance(this, property, null) is not UntypedBindingExpressionBase expression) - throw new NotSupportedException($"Binding returned unsupported {nameof(BindingExpressionBase)}."); - - if (priority != expression.Priority) - { - throw new NotSupportedException( - $"The binding priority passed to AvaloniaObject.Bind ('{priority}') " + - "conflicts with the binding priority of the provided binding expression " + - $" ({expression.Priority}')."); - } - - return GetValueStore().AddBinding(property, expression); - } - - return null; + return _values.AddBinding(property, source, priority); } /// @@ -557,36 +530,13 @@ namespace Avalonia DirectPropertyBase property, IObservable source) { - AvaloniaProperty untypedProperty = property; - - return TryBindDirectPropertyUntyped(ref untypedProperty, source) - ?? _values.AddBinding((DirectPropertyBase)untypedProperty, source); - } - - // Non-generic path extracted to avoid unnecessary generic code duplication - private BindingExpressionBase? TryBindDirectPropertyUntyped( - ref AvaloniaProperty property, - IObservable source) - { - Debug.Assert(property.IsDirect); ThrowHelper.ThrowIfNull(property, nameof(property)); VerifyAccess(); - property = AvaloniaPropertyRegistry.Instance.GetRegisteredDirectUntyped(this, property); - - if (property.IsReadOnly) - { - throw new ArgumentException($"The property {property.Name} is readonly."); - } - - if (source is BindingBase b) - { - if (b.CreateInstance(this, property, null) is not UntypedBindingExpressionBase expression) - throw new NotSupportedException($"Binding returned unsupported {nameof(BindingExpressionBase)}."); - return GetValueStore().AddBinding(property, expression); - } + property = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property); + ThrowIfReadOnly(property); - return null; + return _values.AddBinding(property, source); } /// @@ -606,11 +556,7 @@ namespace Avalonia VerifyAccess(); property = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property); - - if (property.IsReadOnly) - { - throw new ArgumentException($"The property {property.Name} is readonly."); - } + ThrowIfReadOnly(property); return _values.AddBinding(property, source); } @@ -632,11 +578,7 @@ namespace Avalonia VerifyAccess(); property = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property); - - if (property.IsReadOnly) - { - throw new ArgumentException($"The property {property.Name} is readonly."); - } + ThrowIfReadOnly(property); return _values.AddBinding(property, source); } @@ -939,6 +881,14 @@ namespace Avalonia ThrowInvalidPriority(priority); } + private static void ThrowIfReadOnly(AvaloniaProperty property) + { + if (property.IsReadOnly) + { + throw new ArgumentException($"The property {property.Name} is readonly."); + } + } + private static void ThrowInvalidPriority(BindingPriority priority) { throw new ArgumentException($"Invalid priority ${priority}", nameof(priority));