From 5d66bd0c0e7aae7cf7a1f07cd927fbb4bfa1fa28 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 13 Feb 2023 16:55:32 +0100 Subject: [PATCH] Refactored common code into separate method. --- src/Avalonia.Base/StyledProperty.cs | 59 +++++++++++++---------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/Avalonia.Base/StyledProperty.cs b/src/Avalonia.Base/StyledProperty.cs index 8e0ecf5544..ad1f09066e 100644 --- a/src/Avalonia.Base/StyledProperty.cs +++ b/src/Avalonia.Base/StyledProperty.cs @@ -194,45 +194,48 @@ namespace Avalonia } /// - [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConvertionSupressWarningMessage)] internal override IDisposable? RouteSetValue( AvaloniaObject target, object? value, BindingPriority priority) { - if (value == BindingOperations.DoNothing) - { - return null; - } - else if (value == UnsetValue) - { - target.ClearValue(this); - return null; - } - else if (TypeUtilities.TryConvertImplicit(PropertyType, value, out var converted)) - { - return target.SetValue(this, (TValue)converted!, priority); - } - else - { - var type = value?.GetType().FullName ?? "(null)"; - throw new ArgumentException($"Invalid value for Property '{Name}': '{value}' ({type})"); - } + if (ShouldSetValue(target, value, out var converted)) + return target.SetValue(this, converted, priority); + return null; } - [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConvertionSupressWarningMessage)] internal override void RouteSetCurrentValue(AvaloniaObject target, object? value) { - if (value == BindingOperations.DoNothing) - return; + if (ShouldSetValue(target, value, out var converted)) + target.SetCurrentValue(this, converted); + } + + internal override IDisposable RouteBind( + AvaloniaObject target, + IObservable source, + BindingPriority priority) + { + return target.Bind(this, source, priority); + } + [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConvertionSupressWarningMessage)] + private bool ShouldSetValue(AvaloniaObject target, object? value, [NotNullWhen(true)] out TValue? converted) + { + if (value == BindingOperations.DoNothing) + { + converted = default; + return false; + } if (value == UnsetValue) { target.ClearValue(this); + converted = default; + return false; } - else if (TypeUtilities.TryConvertImplicit(PropertyType, value, out var converted)) + else if (TypeUtilities.TryConvertImplicit(PropertyType, value, out var v)) { - target.SetCurrentValue(this, (TValue)converted!); + converted = (TValue)v!; + return true; } else { @@ -241,14 +244,6 @@ namespace Avalonia } } - internal override IDisposable RouteBind( - AvaloniaObject target, - IObservable source, - BindingPriority priority) - { - return target.Bind(this, source, priority); - } - private object? GetDefaultBoxedValue(Type type) { _ = type ?? throw new ArgumentNullException(nameof(type));