示例#1
0
        public static bool TryConvert(
            Type fromType,
            Type toType,
            object sourceValue,
            out object result)
        {
            result = null;
            var enumType   = Choose(fromType, toType, t => t.IsEnum);
            var stringType = Choose(fromType, toType, t => t == typeof(string));

            if (enumType == null || stringType == null)
            {
                return(false);
            }
            if (sourceValue.GetType() != typeof(string))
            {
                // ReSharper disable once ConstantConditionalAccessQualifier
                result = sourceValue?.ToString();
                return(true);
            }
            var method = GenericTryParse.MakeGenericMethod(enumType);
            var args   = new[] { sourceValue, true, null };
            var parsed = (bool)method.Invoke(null, args);

            if (parsed)
            {
                result = args[2];
            }
            return(parsed);
        }
示例#2
0
        public T Read <T>(string section, string key, T defaultValue, GenericTryParse <T> tryParse)
        {
            if (tryParse == null)
            {
                throw new ArgumentNullException("tryParse");
            }

            var valueString = ReadString(section, key, defaultValue.ToString());

            if (string.IsNullOrEmpty(valueString))
            {
                return(defaultValue);
            }

            T value;

            if (tryParse(valueString, out value))
            {
                return(value);
            }
            else
            {
                return(defaultValue);
            }
        }
 internal GenericNumericConversionsDefault(
     string input, GenericTryParse <T> tryParseMethod, GenericTryParseNumeric <T> tryParseNumericMethod, NumberStyles defaultStyles)
 {
     _input                 = input;
     _tryParseMethod        = tryParseMethod;
     _tryParseNumericMethod = tryParseNumericMethod;
     _defaultStyles         = defaultStyles;
 }
示例#4
0
        public static T TryParseDefault <T>(string value, T defaultValue, GenericTryParse <T> method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            T result;

            return(method(value, out result) ? result : defaultValue);
        }
示例#5
0
        public static T?TryParseNullable <T>(string value, GenericTryParse <T> method) where T : struct
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            T result;

            if (!method(value, out result))
            {
                return(null);
            }

            return(result);
        }
        public static T Read <T>(this IniFile2 ini, string section, string key, T defaultValue, GenericTryParse <T> tryParse)
        {
            ValidateIniArgument(ini);

            if (tryParse == null)
            {
                throw new ArgumentNullException("tryParse");
            }

            string valueString = string.Empty;

            if (!ini.TryGet(section, key, ref valueString))
            {
                return(defaultValue);
            }

            T value;

            if (tryParse(valueString, out value))
            {
                return(value);
            }
            else
            {
                return(defaultValue);
            }
        }