Exemplo n.º 1
0
        /// <summary>
        ///     Converts value from one type to another using an optional <see cref="T:System.IFormatProvider" />.
        /// </summary>
        /// <param name="value">The original value.</param>
        /// <param name="type">The target type.</param>
        /// <param name="provider">A <see cref="T:System.IFormatProvider" /> used to format or parse the value.</param>
        /// <param name="format">Format string.</param>
        /// <param name="returnDbNUllIfNotValid">
        ///     Indicates whether exceptions should be avoided or catched and return value should be DBNull if
        ///     it cannot be converted to the target type.
        /// </param>
        /// <returns>The new value in the target type.</returns>
        public static object ChangeType(
            object value,
            Type type,
            IFormatProvider provider,
            string format,
            bool returnDbNUllIfNotValid)
        {
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                value = ChangeType(value, underlyingType, provider, true);
                return(NullableHelperInternal.FixDbNUllasNull(value, type));
            }

            if (value != null && !type.IsAssignableFrom(value.GetType()))
            {
                try
                {
                    if (value is string)
                    {
                        value = format == null || format.Length <= 0
                            ? Parse((string)value, type, provider, "", returnDbNUllIfNotValid)
                            : Parse((string)value, type, provider, format, returnDbNUllIfNotValid);
                    }
                    else if (!(value is DBNull))
                    {
                        if (type.GetTypeInfo().IsEnum)
                        {
                            value = Convert.ChangeType(value, typeof(int), provider);
                            value = Enum.ToObject(type, (int)value);
                        }
                        else
                        {
                            value = !(type == typeof(string)) || value is IConvertible
                                ?
                                    NullableHelperInternal.ChangeType(value, type, provider)
                                : value != null
                                    ? value.ToString()
                                    : "";
                        }
                    }
                }
                catch
                {
                    if (returnDbNUllIfNotValid)
                    {
                        return(DBNull.Value);
                    }
                    throw;
                }
            }

            if ((value == null || value is DBNull) && type == typeof(string))
            {
                return("");
            }
            return(value);
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Parse the given text using the resultTypes "Parse" method or using a type converter.
 /// </summary>
 /// <param name="s">The text to parse.</param>
 /// <param name="resultType">The requested result type.</param>
 /// <param name="provider">A <see cref="T:System.IFormatProvider" /> used to format or parse the value. Can be NULL.</param>
 /// <param name="formats">
 ///     A string array holding permissible formats used in a <see cref="M:System.Object.ToString" /> call. Right now
 ///     formats is only interpreted to enable roundtripping for formatted dates.
 /// </param>
 /// <param name="returnDbNUllIfNotValid">
 ///     Indicates whether DbNull should be returned if value cannot be parsed. Otherwise
 ///     an exception is thrown.
 /// </param>
 /// <returns>The new value in the target type.</returns>
 public static object Parse(
     string s,
     Type resultType,
     IFormatProvider provider,
     string[] formats,
     bool returnDbNUllIfNotValid)
 {
     return(NullableHelperInternal.FixDbNUllasNull(
                _Parse(s, resultType, provider, "", formats, returnDbNUllIfNotValid), resultType));
 }