mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
More cleanup
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user