internal static IValueConverter Create(Type sourceType, Type targetType, bool targetToSource) { if (targetType == typeof(object)) { return(new ObjectTargetConverter(sourceType)); } if (sourceType == typeof(object)) { return(new ObjectSourceConverter(targetType)); } if (SystemConvertConverter.CanConvert(sourceType, targetType)) { return(new SystemConvertConverter(sourceType, targetType)); } Type underlyingType = Nullable.GetUnderlyingType(sourceType); if (underlyingType != null) { sourceType = underlyingType; return(DefaultValueConverter.Create(sourceType, targetType, targetToSource)); } underlyingType = Nullable.GetUnderlyingType(targetType); if (underlyingType != null) { targetType = underlyingType; return(DefaultValueConverter.Create(sourceType, targetType, targetToSource)); } if (sourceType.IsInterface || targetType.IsInterface) { return(new InterfaceConverter(sourceType, targetType)); } TypeConverter converter = DefaultValueConverter.GetConverter(sourceType); bool canConvertTo = converter != null?converter.CanConvertTo(targetType) : false; bool canConvertFrom = converter != null?converter.CanConvertFrom(targetType) : false; if ((canConvertTo || targetType.IsAssignableFrom(sourceType)) && (!targetToSource || canConvertFrom || sourceType.IsAssignableFrom(targetType))) { return(new SourceDefaultValueConverter(converter, sourceType, targetType, !targetToSource ? false : canConvertFrom, canConvertTo)); } converter = DefaultValueConverter.GetConverter(targetType); canConvertTo = converter != null?converter.CanConvertTo(sourceType) : false; canConvertFrom = converter != null?converter.CanConvertFrom(sourceType) : false; if ((!canConvertFrom && !targetType.IsAssignableFrom(sourceType)) || (targetToSource && !canConvertTo && !sourceType.IsAssignableFrom(targetType))) { CultureInfo invariantCulture = CultureInfo.InvariantCulture; string str = "ValueConverter can't convert Type {0} to Type {1}"; object[] objArray = new object[] { sourceType.ToString(), targetType.ToString() }; throw new InvalidOperationException(string.Format(invariantCulture, str, objArray)); } return(new TargetDefaultValueConverter(converter, sourceType, targetType, canConvertFrom, !targetToSource ? false : canConvertTo)); }
public static bool CanConvert(Type sourceType, Type targetType) { if (sourceType == typeof(DateTime)) { return(targetType == typeof(string)); } if (targetType == typeof(DateTime)) { return(sourceType == typeof(string)); } if (sourceType == typeof(char)) { return(SystemConvertConverter.CanConvertChar(targetType)); } if (targetType == typeof(char)) { return(SystemConvertConverter.CanConvertChar(sourceType)); } for (int i = 0; i < (int)SystemConvertConverter.SupportedTypes.Length; i++) { if (sourceType == SystemConvertConverter.SupportedTypes[i]) { for (i++; i < (int)SystemConvertConverter.SupportedTypes.Length; i++) { if (targetType == SystemConvertConverter.SupportedTypes[i]) { return(true); } } } else if (targetType == SystemConvertConverter.SupportedTypes[i]) { for (i++; i < (int)SystemConvertConverter.SupportedTypes.Length; i++) { if (sourceType == SystemConvertConverter.SupportedTypes[i]) { return(true); } } } } return(false); }