Beispiel #1
0
        /// <summary>
        /// Converts a text into the given type. If the type is a boolean, then the conversions in
        /// <see cref="BooleanUtilities"/> will be used.
        /// </summary>
        /// <typeparam name="T">The type to which the string is converted.</typeparam>
        /// <param name="s">The string, which is converted into the specified type.</param>
        /// <returns>A values representing the interpretation of the provided string.</returns>
        public static T ConvertString <T>(string s)
        {
            var tType = typeof(T);

            if (tType == typeof(bool))
            {
                object b = BooleanUtilities.ToBool(s);
                return((T)b);
            }
            return((T)Convert.ChangeType(s, tType));
        }
Beispiel #2
0
        /// <summary>
        /// <para>Parses the provided string into the specified type. This is done in the following way:</para>
        /// <para>1.	If the provided value is null, then an <see cref="NullParameterException"/> is thrown.</para>
        /// <para>2.	If the specified type is an enum type, then the provided value is parsed into that enum value and returned.</para>
        /// <para>3.	If the specified type is not contained in <see cref="TypeNames"/> then an <see cref="UnsupportedTypeException"/> is thrown.</para>
        /// <para>4.	If the specified type is a string, then the provided string value is returned.</para>
        /// <para>5.	If the specified type is a boolean, then the provided string value is converted to the boolean value.</para>
        /// <para>6.	If the specified type can be mapped to an entry in <see cref="NumericParsers"/>, then that parser is used to parse the provided string</para>
        /// <para>value into a numeric value and the numeric value is returned.</para>
        /// <para>Otherwise an <see cref="UnsupportedTypeException"/> is thrown.</para>
        /// </summary>
        /// <param name="value">The value, which will be parsed.</param>
        /// <param name="typeName">The name of the type, which is used to determine how the parsing is performed.</param>
        /// <returns>The result of the parsing.</returns>
        public static object ParseToType(string value, string typeName)
        {
            if (value == null)
            {
                throw new NullParameterException("value");
            }

            if (!TypeNames.ContainsKey(typeName))
            {
                var type = GetType(typeName);
                if (type == null)
                {
                    throw new UnsupportedTypeException(value, typeName);
                }

                if (type.IsEnum)
                {
                    return(Enum.Parse(type, value));
                }

                throw new UnsupportedTypeException(value, typeName);
            }

            {
                var type = TypeNames[typeName];

                if (type == typeof(string))
                {
                    return(value);
                }

                if (type == typeof(bool))
                {
                    return(BooleanUtilities.ToBool(value));
                }

                if (NumericParsers.ContainsKey(type))
                {
                    return(NumericParsers[type](value));
                }
            }

            throw new UnsupportedTypeException(value, typeName);
        }