public static bool CanConvertType(Type initialType, Type targetType, bool allowTypeNameToString) { ValidationUtils.ArgumentNotNull(initialType, "initialType"); ValidationUtils.ArgumentNotNull(targetType, "targetType"); if (ReflectionUtils.IsNullableType(targetType)) { targetType = Nullable.GetUnderlyingType(targetType); } if (targetType == initialType) { return(true); } if (typeof(IConvertible).IsAssignableFrom(initialType) && typeof(IConvertible).IsAssignableFrom(targetType)) { return(true); } if (initialType == typeof(DateTime) && targetType == typeof(DateTimeOffset)) { return(true); } if (initialType == typeof(Guid) && (targetType == typeof(Guid) || targetType == typeof(string))) { return(true); } if (initialType == typeof(Type) && targetType == typeof(string)) { return(true); } #if !PocketPC // see if source or target types have a TypeConverter that converts between the two TypeConverter toConverter = GetConverter(initialType); if (toConverter != null && !IsComponentConverter(toConverter) && toConverter.CanConvertTo(targetType)) { if (allowTypeNameToString || toConverter.GetType() != typeof(TypeConverter)) { return(true); } } TypeConverter fromConverter = GetConverter(targetType); if (fromConverter != null && !IsComponentConverter(fromConverter) && fromConverter.CanConvertFrom(initialType)) { return(true); } #endif // handle DBNull and INullable if (initialType == typeof(DBNull)) { if (ReflectionUtils.IsNullable(targetType)) { return(true); } } return(false); }
/// <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); } if (initialValue is string && typeof(Type).IsAssignableFrom(targetType)) { return(Type.GetType((string)initialValue, true)); } 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"); } // use Convert.ChangeType if both types are IConvertible if (initialValue is IConvertible && typeof(IConvertible).IsAssignableFrom(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 (initialValue is DateTime && targetType == typeof(DateTimeOffset)) { return(new DateTimeOffset((DateTime)initialValue)); } if (initialValue is string) { if (targetType == typeof(Guid)) { return(new Guid((string)initialValue)); } if (targetType == typeof(Uri)) { return(new Uri((string)initialValue)); } } #if !PocketPC // 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 // 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)); } #if !SILVERLIGHT if (initialValue is INullable) { return(EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType)); } #endif throw new Exception("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType)); }