From 8e307df231e9709432c2178d1079aedd41a6f9d5 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Mon, 10 Jan 2022 16:55:43 +0200 Subject: [PATCH] More cleanup --- CliFx/CommandBinder.cs | 12 ++++++------ CliFx/Utils/Extensions/TypeExtensions.cs | 23 ----------------------- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/CliFx/CommandBinder.cs b/CliFx/CommandBinder.cs index a1a6dc2..27740f4 100644 --- a/CliFx/CommandBinder.cs +++ b/CliFx/CommandBinder.cs @@ -43,12 +43,6 @@ internal class CommandBinder return string.IsNullOrWhiteSpace(rawValue) || bool.Parse(rawValue); } - // IConvertible primitives (int, double, char, etc) - if (targetType.IsConvertible()) - { - return Convert.ChangeType(rawValue, targetType, _formatProvider); - } - // Special case for DateTimeOffset if (targetType == typeof(DateTimeOffset)) { @@ -68,6 +62,12 @@ internal class CommandBinder return Enum.Parse(targetType, rawValue!, true); } + // Convertible primitives (int, double, char, etc) + if (targetType.Implements(typeof(IConvertible))) + { + return Convert.ChangeType(rawValue, targetType, _formatProvider); + } + // Nullable var nullableUnderlyingType = targetType.TryGetNullableUnderlyingType(); if (nullableUnderlyingType is not null) diff --git a/CliFx/Utils/Extensions/TypeExtensions.cs b/CliFx/Utils/Extensions/TypeExtensions.cs index d8c71b1..8f05cc7 100644 --- a/CliFx/Utils/Extensions/TypeExtensions.cs +++ b/CliFx/Utils/Extensions/TypeExtensions.cs @@ -59,27 +59,4 @@ internal static class TypeExtensions var toStringMethod = type.GetMethod(nameof(ToString), Type.EmptyTypes); return toStringMethod?.GetBaseDefinition()?.DeclaringType != toStringMethod?.DeclaringType; } - - // Types supported by `Convert.ChangeType(...)` - private static readonly HashSet ConvertibleTypes = new() - { - typeof(bool), - typeof(char), - typeof(sbyte), - typeof(byte), - typeof(short), - typeof(ushort), - typeof(int), - typeof(uint), - typeof(long), - typeof(ulong), - typeof(float), - typeof(double), - typeof(decimal), - typeof(DateTime), - typeof(string), - typeof(object) - }; - - public static bool IsConvertible(this Type type) => ConvertibleTypes.Contains(type); } \ No newline at end of file