Esempio n. 1
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>();
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to create an instance.
        /// </summary>
        internal static bool TryCreate(out IValueConverter <TValue>?converter)
        {
            converter = null;
            var valueType = typeof(TValue);

            if (!TypeHelpers.TryGetParseMethodInfo(valueType, out var parseMethodInfo))
            {
                return(false);
            }

            var strParamExpr  = Expression.Parameter(typeof(string));
            var callParseExpr = Expression.Call(parseMethodInfo !, strParamExpr);
            var lambdaExpr    = Expression.Lambda <Func <string, TValue> >(callParseExpr, strParamExpr);
            var function      = lambdaExpr.Compile();

            converter = new ParseConverter <TValue>(function);
            return(true);
        }