Example #1
0
        /// <summary>
        /// Converts the value to the specified type. If the value is unable to be converted, the
        /// value is checked whether it assignable to the specified type.
        /// </summary>
        /// <param name="initialValue">The value to convert.</param>
        /// <param name="culture">The culture to use when converting.</param>
        /// <param name="targetType">The type to convert or cast the value to.</param>
        /// <returns>
        /// The converted type. If conversion was unsuccessful, the initial value
        /// is returned if assignable to the target type.
        /// </returns>
        public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType)
        {
            object convertedValue;

            if (TryConvert(initialValue, culture, targetType, out convertedValue))
            {
                return(convertedValue);
            }

            return(EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType));
        }
Example #2
0
        /// <summary>
        /// Converts the value to the specified type. If the value is unable to be converted, the
        /// value is checked whether it assignable to the specified type.
        /// </summary>
        /// <param name="initialValue">The value to convert.</param>
        /// <param name="culture">The culture to use when converting.</param>
        /// <param name="targetType">The type to convert or cast the value to.</param>
        /// <returns>
        /// The converted type. If conversion was unsuccessful, the initial value
        /// is returned if assignable to the target type.
        /// </returns>
        public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType)
        {
            object convertedValue;

            if (targetType == typeof(object))
                return initialValue;

            if (initialValue == null && ReflectionUtils.IsNullable(targetType))
                return null;

            if (TryConvert(initialValue, culture, targetType, out convertedValue))
                return convertedValue;

            return EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType);
        }
Example #3
0
 public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType)
 {
     if (targetType == typeof(object))
     {
         return(initialValue);
     }
     if ((initialValue == null) && ReflectionUtils.IsNullable(targetType))
     {
         return(null);
     }
     if (TryConvert(initialValue, culture, targetType, out object obj2))
     {
         return(obj2);
     }
     return(EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType));
 }
Example #4
0
        public static object ConvertOrCast([Nullable(2)] object initialValue, CultureInfo culture, Type targetType)
        {
            if (targetType == typeof(object))
            {
                return(initialValue);
            }
            if (initialValue == null && ReflectionUtils.IsNullable(targetType))
            {
                return(null);
            }
            object result;

            if (ConvertUtils.TryConvert(initialValue, culture, targetType, out result))
            {
                return(result);
            }
            return(ConvertUtils.EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType));
        }
Example #5
0
        public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType)
        {
            if (targetType == typeof(object))
            {
                return(initialValue);
            }
            if (initialValue == null && ReflectionUtils.IsNullable(targetType))
            {
                return((object)null);
            }
            object convertedValue;

            if (ConvertUtils.TryConvert(initialValue, culture, targetType, out convertedValue))
            {
                return(convertedValue);
            }
            else
            {
                return(ConvertUtils.EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType));
            }
        }