More cleanup

This commit is contained in:
Tyrrrz
2022-01-10 16:55:43 +02:00
parent ff38f4916a
commit 8e307df231
2 changed files with 6 additions and 29 deletions

View File

@@ -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<T>
var nullableUnderlyingType = targetType.TryGetNullableUnderlyingType();
if (nullableUnderlyingType is not null)

View File

@@ -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<Type> 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);
}