Beispiel #1
0
        /// <summary>
        /// Converts the value 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 the value to.</param>
        /// <returns>The converted type.</returns>
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }

            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }

            Type initialType = initialValue.GetType();

            if (targetType == initialType)
            {
                return(initialValue);
            }

            // use Convert.ChangeType if both types are IConvertible
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        return(Enum.Parse(targetType, initialValue.ToString(), true));
                    }
                    else if (IsInteger(initialValue))
                    {
                        return(Enum.ToObject(targetType, initialValue));
                    }
                }

                return(System.Convert.ChangeType(initialValue, targetType, culture));
            }

#if !NET20
            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset((DateTime)initialValue));
            }
#endif

            if (initialValue is byte[] && targetType == typeof(Guid))
            {
                return(new Guid((byte[])initialValue));
            }

            if (initialValue is string)
            {
                if (targetType == typeof(Guid))
                {
                    return(new Guid((string)initialValue));
                }
                if (targetType == typeof(Uri))
                {
                    return(new Uri((string)initialValue, UriKind.RelativeOrAbsolute));
                }
                if (targetType == typeof(TimeSpan))
                {
                    return(ParseTimeSpan((string)initialValue));
                }
                if (typeof(Type).IsAssignableFrom(targetType))
                {
                    return(Type.GetType((string)initialValue, true));
                }
            }

#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
            if (targetType == typeof(BigInteger))
            {
                return(ToBigInteger(initialValue));
            }
            if (initialValue is BigInteger)
            {
                return(FromBigInteger((BigInteger)initialValue, targetType));
            }
#endif

#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
            // see if source or target types have a TypeConverter that converts between the two
            TypeConverter toConverter = GetConverter(initialType);

            if (toConverter != null && toConverter.CanConvertTo(targetType))
            {
#if !SILVERLIGHT
                return(toConverter.ConvertTo(null, culture, initialValue, targetType));
#else
                return(toConverter.ConvertTo(initialValue, targetType));
#endif
            }

            TypeConverter fromConverter = GetConverter(targetType);

            if (fromConverter != null && fromConverter.CanConvertFrom(initialType))
            {
#if !SILVERLIGHT
                return(fromConverter.ConvertFrom(null, culture, initialValue));
#else
                return(fromConverter.ConvertFrom(initialValue));
#endif
            }
#endif
#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
            // handle DBNull and INullable
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    return(EnsureTypeAssignable(null, initialType, targetType));
                }

                throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
            }
#endif
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE)
            if (initialValue is INullable)
            {
                return(EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType));
            }
#endif

            if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract())
            {
                throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType");
            }

            throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
        }