4using System.Collections.Generic;
5using System.Globalization;
7using System.Reflection;
23 CultureInfo conversionCulture,
27 scalar ?
ChangeTypeScalar(values.Last(), conversionType, conversionCulture, ignoreValueCase) :
33 CultureInfo conversionCulture,
37 conversionType.GetTypeInfo()
38 .GetGenericArguments()
42 InvalidOperationException(
"Non scalar properties should be sequence of type IEnumerable<T>."
46 IEnumerable<Maybe<object>> converted =
47 values.Select(value =>
ChangeTypeScalar(value, type, conversionCulture, ignoreValueCase));
49 return converted.Any(a => a.MatchNothing())
50 ?
Maybe.Nothing<
object>()
58 CultureInfo conversionCulture,
64 result.Match((_, __) => { },
66 .RethrowWhenAbsentIn(
new[]
68 typeof(InvalidCastException),
69 typeof(FormatException),
70 typeof(OverflowException),
75 return result.ToMaybe();
80 CultureInfo conversionCulture,
83 IEnumerable<Maybe<object>> converted =
84 values.Select(value =>
ChangeTypeScalar(value, typeof(
bool), conversionCulture, ignoreValueCase));
86 return converted.Any(maybe => maybe.MatchNothing())
87 ?
Maybe.Nothing<
object>()
88 :
Maybe.Just((
object)converted.Count(value => value.IsJust()));
91 private static object ConvertString(
string value, Type type, CultureInfo conversionCulture)
95 return Convert.ChangeType(value, type, conversionCulture);
97 catch (InvalidCastException)
100 return System.ComponentModel.TypeDescriptor.GetConverter(type)
101 .ConvertFrom(
null, conversionCulture, value);
107 CultureInfo conversionCulture,
108 bool ignoreValueCase)
110 Func<object> changeType = () =>
112 Func<object> safeChangeType = () =>
116 Func<Type> getUnderlyingType =
122 Nullable.GetUnderlyingType(conversionType);
124 Type type = getUnderlyingType() ?? conversionType;
126 Func<object> withValue =
136 Func<object> empty = () =>
null;
139 return value ==
null ? empty() : withValue();
142 return value.IsBooleanString() && conversionType == typeof(
bool) ? value.ToBoolean() :
143 conversionType.GetTypeInfo()
144 .IsEnum ? value.ToEnum(conversionType, ignoreValueCase) : safeChangeType();
147 Func<object> makeType = () =>
151 ConstructorInfo ctor = conversionType.GetTypeInfo()
152 .GetConstructor(
new[] { typeof(
string) });
154 return ctor.Invoke(
new object[] { value });
159 FormatException(
"Destination conversion type must have a constructor that accepts a string.");
163 if (conversionType.IsCustomStruct())
165 return Result.Try(makeType);
174 private static object ToEnum(
this string value, Type conversionType,
bool ignoreValueCase)
180 parsedValue = Enum.Parse(conversionType, value, ignoreValueCase);
182 catch (ArgumentException)
184 throw new FormatException();
192 throw new FormatException();
197 char firstChar = enumValue.ToString()[0];
199 if (
char.IsDigit(firstChar) || firstChar ==
'-')
Models a CSharpx.Maybe when contains a value.
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
static bool IsDefinedEx(object enumValue)
static object ToEnum(this string value, Type conversionType, bool ignoreValueCase)
static Maybe< object > ChangeTypeSequence(IEnumerable< string > values, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
static Result< object, Exception > ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
static Maybe< object > ChangeTypeScalar(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
static Maybe< object > ChangeTypeFlagCounter(IEnumerable< string > values, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
static object ConvertString(string value, Type type, CultureInfo conversionCulture)
static Maybe< object > ChangeType(IEnumerable< string > values, Type conversionType, bool scalar, bool isFlag, CultureInfo conversionCulture, bool ignoreValueCase)
static object None(Type type)
static Type GetUnderlyingType(Type type)
static object Some(Type type, object value)
static bool IsFSharpOptionType(Type type)
Represents the result of a computation.