Exemple #1
0
 private static bool TryConvertCore(object value, Type destinationType, ref object result, CultureInfo culture, ConversionOptions options)
 {
     if (value.GetType() == destinationType)
     {
         result = value;
         return(true);
     }
     if (TryConvertByDefaultTypeConverters(value, destinationType, culture, ref result))
     {
         return(true);
     }
     if (TryConvertByIConvertibleImplementation(value, destinationType, culture, ref result))
     {
         return(true);
     }
     if (TryConvertXPlicit(value, destinationType, ExplicitOperatorMethodName, ref result))
     {
         return(true);
     }
     if (TryConvertXPlicit(value, destinationType, ImplicitOperatorMethodName, ref result))
     {
         return(true);
     }
     if (TryConvertByIntermediateConversion(value, destinationType, ref result, culture, options))
     {
         return(true);
     }
     if (destinationType.IsEnum)
     {
         if (TryConvertToEnum(value, destinationType, ref result))
         {
             return(true);
         }
     }
     if ((options & ConversionOptions.EnhancedTypicalValues) == ConversionOptions.EnhancedTypicalValues)
     {
         if (TryConvertSpecialValues(value, destinationType, ref result))
         {
             return(true);
         }
     }
     if ((options & ConversionOptions.AllowDefaultValueIfWhitespace) == ConversionOptions.AllowDefaultValueIfWhitespace)
     {
         if (value is string)
         {
             if (IsWhiteSpace((string)value))
             {
                 result = GetDefaultValueOfType(destinationType);
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #2
0
 private static bool TryConvertFromNull(Type destinationType, out object result, ConversionOptions options)
 {
     result = GetDefaultValueOfType(destinationType);
     if (result == null)
     {
         return(true);
     }
     return((options & ConversionOptions.AllowDefaultValueIfNull) ==
            ConversionOptions.AllowDefaultValueIfNull);
 }
Exemple #3
0
 private static bool TryConvertByIntermediateConversion(object value, Type destinationType, ref object result, CultureInfo culture, ConversionOptions options)
 {
     if (value is char &&
         (destinationType == typeof(double) || destinationType == typeof(float)))
     {
         return(TryConvertCore(System.Convert.ToInt16(value), destinationType, ref result, culture, options));
     }
     if ((value is double || value is float) && destinationType == typeof(char))
     {
         return(TryConvertCore(System.Convert.ToInt16(value), destinationType, ref result, culture, options));
     }
     return(false);
 }
        /// <summary>
        /// Determines whether the given value can be converted to the specified type using the given CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
        /// </summary>
        /// <typeparam name="T">The Type to test.</typeparam>
        /// <param name="value">The value to test.</param>
        /// <param name="culture">The CultureInfo to use as the current culture.</param>
        /// <param name="options">The options which are used for conversion.</param>
        /// <returns>true if <paramref name="value"/> can be converted to <typeparamref name="T"/>; otherwise, false.</returns>
        public static bool CanConvertTo <T>(object value, CultureInfo culture, ConversionOptions options)
        {
            T result;

            return(TryConvertTo(value, out result, culture, options));
        }
 /// <summary>
 /// Defines options used for conversion.
 /// </summary>
 /// <param name="options">The options which are used for conversion.</param>
 /// <returns>This instance.</returns>
 public EnumerableConversion <T> UsingConversionOptions(ConversionOptions options)
 {
     mConversionOptions = options;
     return(this);
 }
 /// <summary>
 /// Converts the given value to the given type using the current CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
 /// </summary>
 /// <param name="value">The value which is converted.</param>
 /// <param name="destinationType">The type to which the given value is converted.</param>
 /// <param name="options">The options which are used for conversion.</param>
 /// <returns>An Object instance of type <paramref name="destinationType">destinationType</paramref> whose value is equivalent to the given <paramref name="value">value</paramref>.</returns>
 public static object Convert(object value, Type destinationType, ConversionOptions options)
 {
     return(Convert(value, destinationType, DefaultCulture, options));
 }
        /// <summary>
        /// Converts the given value to the given type using the given CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
        /// </summary>
        /// <param name="value">The value which is converted.</param>
        /// <param name="destinationType">The type to which the given value is converted.</param>
        /// <param name="culture">The CultureInfo to use as the current culture.</param>
        /// <param name="options">The options which are used for conversion.</param>
        /// <returns>An Object instance of type <paramref name="destinationType">destinationType</paramref> whose value is equivalent to the given <paramref name="value">value</paramref>.</returns>
        public static object Convert(object value, Type destinationType, CultureInfo culture, ConversionOptions options)
        {
            object result;

            if (TryConvert(value, destinationType, out result, culture, options))
            {
                return(result);
            }
            throw new InvalidConversionException(value, destinationType);
        }
 /// <summary>
 /// Converts the given value to the given type using the current CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
 /// A return value indicates whether the operation succeeded.
 /// </summary>
 /// <param name="value">The value which is converted.</param>
 /// <param name="destinationType">The type to which the given value is converted.</param>
 /// <param name="result">An Object instance of type <paramref name="destinationType">destinationType</paramref> whose value is equivalent to the given <paramref name="value">value</paramref> if the operation succeeded.</param>
 /// <param name="options">The options which are used for conversion.</param>
 /// <returns>true if <paramref name="value"/> was converted successfully; otherwise, false.</returns>
 public static bool TryConvert(object value, Type destinationType, out object result, ConversionOptions options)
 {
     return(TryConvert(value, destinationType, out result, DefaultCulture, options));
 }
        /// <summary>
        /// Converts the given value to the given type using the given CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
        /// A return value indicates whether the operation succeeded.
        /// </summary>
        /// <param name="value">The value which is converted.</param>
        /// <param name="destinationType">The type to which the given value is converted.</param>
        /// <param name="result">An Object instance of type <paramref name="destinationType">destinationType</paramref> whose value is equivalent to the given <paramref name="value">value</paramref> if the operation succeeded.</param>
        /// <param name="culture">The CultureInfo to use as the current culture.</param>
        /// <param name="options">The options which are used for conversion.</param>
        /// <returns>true if <paramref name="value"/> was converted successfully; otherwise, false.</returns>
        public static bool TryConvert(object value, Type destinationType, out object result, CultureInfo culture, ConversionOptions options)
        {
            if (destinationType == typeof(object))
            {
                result = value;
                return(true);
            }
            if (ValueRepresentsNull(value))
            {
                return(TryConvertFromNull(destinationType, out result, options));
            }
            if (destinationType.IsAssignableFrom(value.GetType()))
            {
                result = value;
                return(true);
            }
            Type   coreDestinationType = IsGenericNullable(destinationType) ? GetUnderlyingType(destinationType) : destinationType;
            object tmpResult           = null;

            if (TryConvertCore(value, coreDestinationType, ref tmpResult, culture, options))
            {
                result = tmpResult;
                return(true);
            }
            result = null;
            return(false);
        }
        /// <summary>
        /// Determines whether the given value can be converted to the specified type using the given CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
        /// </summary>
        /// <param name="value">The value to test.</param>
        /// <param name="destinationType">The Type to test.</param>
        /// <param name="culture">The CultureInfo to use as the current culture.</param>
        /// <param name="options">The options which are used for conversion.</param>
        /// <returns>true if <paramref name="value"/> can be converted to <paramref name="destinationType"/>; otherwise, false.</returns>
        public static bool CanConvert(object value, Type destinationType, CultureInfo culture, ConversionOptions options)
        {
            object result;

            return(TryConvert(value, destinationType, out result, culture, options));
        }
 /// <summary>
 /// Converts the given value to the given type using the given CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
 /// </summary>
 /// <typeparam name="T">The type to which the given value is converted.</typeparam>
 /// <param name="value">The value which is converted.</param>
 /// <param name="culture">The CultureInfo to use as the current culture.</param>
 /// <param name="options">The options which are used for conversion.</param>
 /// <returns>An Object instance of type <typeparamref name="T">T</typeparamref> whose value is equivalent to the given <paramref name="value">value</paramref>.</returns>
 public static T ConvertTo <T>(object value, CultureInfo culture, ConversionOptions options)
 {
     return((T)Convert(value, typeof(T), culture, options));
 }
 /// <summary>
 /// Converts the given value to the given type using the current CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
 /// </summary>
 /// <typeparam name="T">The type to which the given value is converted.</typeparam>
 /// <param name="value">The value which is converted.</param>
 /// <param name="options">The options which are used for conversion.</param>
 /// <returns>An Object instance of type <typeparamref name="T">T</typeparamref> whose value is equivalent to the given <paramref name="value">value</paramref>.</returns>
 public static T ConvertTo <T>(object value, ConversionOptions options)
 {
     return(ConvertTo <T>(value, DefaultCulture, options));
 }
        /// <summary>
        /// Converts the given value to the given type using the given CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
        /// A return value indicates whether the operation succeeded.
        /// </summary>
        /// <typeparam name="T">The type to which the given value is converted.</typeparam>
        /// <param name="value">The value which is converted.</param>
        /// <param name="result">An Object instance of type <typeparamref name="T">T</typeparamref> whose value is equivalent to the given <paramref name="value">value</paramref> if the operation succeeded.</param>
        /// <param name="culture">The CultureInfo to use as the current culture.</param>
        /// <param name="options">The options which are used for conversion.</param>
        /// <returns>true if <paramref name="value"/> was converted successfully; otherwise, false.</returns>
        public static bool TryConvertTo <T>(object value, out T result, CultureInfo culture, ConversionOptions options)
        {
            object tmpResult;

            if (TryConvert(value, typeof(T), out tmpResult, culture, options))
            {
                result = (T)tmpResult;
                return(true);
            }
            result = default(T);
            return(false);
        }
 /// <summary>
 /// Converts the given value to the given type using the current CultureInfo and the given <see cref="ConversionOptions">ConversionOptions</see>.
 /// A return value indicates whether the operation succeeded.
 /// </summary>
 /// <typeparam name="T">The type to which the given value is converted.</typeparam>
 /// <param name="value">The value which is converted.</param>
 /// <param name="result">An Object instance of type <typeparamref name="T">T</typeparamref> whose value is equivalent to the given <paramref name="value">value</paramref> if the operation succeeded.</param>
 /// <param name="options">The options which are used for conversion.</param>
 /// <returns>true if <paramref name="value"/> was converted successfully; otherwise, false.</returns>
 public static bool TryConvertTo <T>(object value, out T result, ConversionOptions options)
 {
     return(TryConvertTo(value, out result, DefaultCulture, options));
 }