Exemplo n.º 1
0
        /// <summary>
        /// Tries to create an instance.
        /// </summary>
        internal static bool TryCreate(out IValueConverter <TValue>?converter)
        {
            converter = null;
            var valueType = typeof(TValue);

            if (!TypeHelpers.TryGetStringConstructor(valueType, out var constructor))
            {
                return(false);
            }

            var strParamExpr = Expression.Parameter(typeof(string));
            var newExpr      = Expression.New(constructor !, strParamExpr);
            var lambdaExpr   = Expression.Lambda <Func <string, TValue> >(newExpr, strParamExpr);
            var function     = lambdaExpr.Compile();

            converter = new ConstructorConverter <TValue>(function);
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates or throws a converter.
        /// </summary>
        internal static IValueConverter <TValue> CreateOrThrow <TValue>()
        {
            var created =
                StringConverter <TValue> .TryCreate(out var converter) ||
                ParseConverter <TValue> .TryCreate(out converter) ||
                EnumConverter <TValue> .TryCreate(out converter) ||
                NullableTypeParseConverter <TValue> .TryCreate(out converter) ||
                NullableEnumConverter <TValue> .TryCreate(out converter) ||
                ConstructorConverter <TValue> .TryCreate(out converter) ||
                CastConverter <TValue> .TryCreate(out converter);

            if (created)
            {
                return(converter !);
            }

            throw ConfigurationExceptions.NoDefaultConverter <TValue>();
        }