Browse Source

Refactored common code into separate method.

Steven Kirk 2 years ago
parent
commit
5d66bd0c0e
1 changed files with 27 additions and 32 deletions
  1. 27 32
      src/Avalonia.Base/StyledProperty.cs

+ 27 - 32
src/Avalonia.Base/StyledProperty.cs

@@ -194,45 +194,48 @@ namespace Avalonia
         }
 
         /// <inheritdoc/>
-        [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<TValue>(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<TValue>(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<TValue>(this, converted);
+        }
+
+        internal override IDisposable RouteBind(
+            AvaloniaObject target,
+            IObservable<object?> source,
+            BindingPriority priority)
+        {
+            return target.Bind<TValue>(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<TValue>(this, (TValue)converted!);
+                converted = (TValue)v!;
+                return true;
             }
             else
             {
@@ -241,14 +244,6 @@ namespace Avalonia
             }
         }
 
-        internal override IDisposable RouteBind(
-            AvaloniaObject target,
-            IObservable<object?> source,
-            BindingPriority priority)
-        {
-            return target.Bind<TValue>(this, source, priority);
-        }
-
         private object? GetDefaultBoxedValue(Type type)
         {
             _ = type ?? throw new ArgumentNullException(nameof(type));