Esempio n. 1
0
        private static Either <object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture)
        {
            Func <string, object> changeType = input =>
            {
                Func <object> safeChangeType = () =>
                {
                    var isFsOption = ReflectionHelper.IsFSharpOptionType(conversionType);

                    Func <Type> getUnderlyingType =
                        () =>
                        isFsOption
                                ? FSharpOptionHelper.GetUnderlyingType(conversionType)
                                : Nullable.GetUnderlyingType(conversionType);

                    var type = getUnderlyingType() ?? conversionType;

                    Func <object> withValue =
                        () =>
                        isFsOption
                                ? FSharpOptionHelper.Some(type, Convert.ChangeType(input, type, conversionCulture))
                                : Convert.ChangeType(input, type, conversionCulture);

                    Func <object> empty = () => isFsOption?FSharpOptionHelper.None(type) : null;

                    return((input == null) ? empty() : withValue());
                };

                return(input.IsBooleanString()
                    ? input.ToBoolean() : conversionType.IsEnum
                        ? input.ToEnum(conversionType) : safeChangeType());
            };

            return(Either.Protect(changeType, value));
        }
Esempio n. 2
0
        private static Result <object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase)
        {
            Func <object> changeType = () =>
            {
                Func <object> safeChangeType = () =>
                {
                    var isFsOption = ReflectionHelper.IsFSharpOptionType(conversionType);

                    Func <Type> getUnderlyingType =
                        () =>
#if !SKIP_FSHARP
                        isFsOption
                                ? FSharpOptionHelper.GetUnderlyingType(conversionType) :
#endif
                        Nullable.GetUnderlyingType(conversionType);

                    var type = getUnderlyingType() ?? conversionType;

                    Func <object> withValue =
                        () =>
#if !SKIP_FSHARP
                        isFsOption
                                ? FSharpOptionHelper.Some(type, ConvertString(value, type, conversionCulture)) :
#endif
                        ConvertString(value, type, conversionCulture);

#if !SKIP_FSHARP
                    Func <object> empty = () => isFsOption?FSharpOptionHelper.None(type) : null;
#else
                    Func <object> empty = () => null;
#endif

                    return((value == null) ? empty() : withValue());
                };

                return(value.IsBooleanString() && conversionType == typeof(bool)
                    ? value.ToBoolean() : conversionType.GetTypeInfo().IsEnum
                        ? value.ToEnum(conversionType, ignoreValueCase) : safeChangeType());
            };

            Func <object> makeType = () =>
            {
                try
                {
                    var ctor = conversionType.GetTypeInfo().GetConstructor(new[] { typeof(string) });
                    return(ctor.Invoke(new object[] { value }));
                }
                catch (Exception)
                {
                    throw new FormatException("Destination conversion type must have a constructor that accepts a string.");
                }
            };

            return(Result.Try(
                       conversionType.IsPrimitiveEx() || ReflectionHelper.IsFSharpOptionType(conversionType)
                    ? changeType
                    : makeType));
        }
Esempio n. 3
0
        private static Maybe <object> ChangeType(string value, Type conversionType, CultureInfo conversionCulture)
        {
            try
            {
                Func <object> safeChangeType = () =>
                {
                    var isFsOption = ReflectionHelper.IsFSharpOptionType(conversionType);

                    Func <Type> getUnderlyingType = () =>
                                                    isFsOption
                                ? FSharpOptionHelper.GetUnderlyingType(conversionType)
                                : Nullable.GetUnderlyingType(conversionType);

                    var type = getUnderlyingType() ?? conversionType;

                    Func <object> withValue = () =>
                                              isFsOption
                                ? FSharpOptionHelper.Some(type, Convert.ChangeType(value, type, conversionCulture))
                                : Convert.ChangeType(value, type, conversionCulture);

                    Func <object> empty = () =>
                                          isFsOption
                                ? FSharpOptionHelper.None(type)
                                : null;

                    return((value == null) ? empty() : withValue());
                };

                return(Maybe.Just(
                           MatchBoolString(value)
                        ? ConvertBoolString(value)
                        : conversionType.IsEnum
                            ? ConvertEnumString(value, conversionType)
                            : safeChangeType()));
            }
            catch (InvalidCastException)
            {
                return(Maybe.Nothing <object>());
            }
            catch (FormatException)
            {
                return(Maybe.Nothing <object>());
            }
            catch (OverflowException)
            {
                return(Maybe.Nothing <object>());
            }
        }
Esempio n. 4
0
        public void Create_none()
        {
            var expected = FSharpOptionHelper.None(FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType));

            FSharpOption <string> .get_IsNone((FSharpOption <string>) expected).Should().BeTrue();
        }