Beispiel #1
0
        private static Tuple <Type, Type> MatchTypeConverter(Type targetType, ReflectedProperty property, Type fromType, Tuple <Type, Type> typeConverter, bool autoTypeCoercion)
        {
            if (typeConverter.Item1 == null && autoTypeCoercion)
            {
                var interfaceType = typeConverter.Item2;
                if (interfaceType == null)
                {
                    interfaceType = TypeConverterAttribute.GetExpectedConverterInterfaceType(fromType, property.PropertyType);
                }

                if (property.PropertyType == typeof(string))
                {
                    return(Tuple.Create(typeof(DBNullableStringConverter), interfaceType));
                }
                else if (property.IsNullableType)
                {
                    if (property.PropertyType.IsEnum)
                    {
                        var propertyType = property.PropertyType.GetEnumUnderlyingType();
                        return(Tuple.Create(typeof(NullableEnumConverter <>).MakeGenericType(propertyType), interfaceType));
                    }
                    else
                    {
                        var propertyType = Nullable.GetUnderlyingType(property.PropertyType);
                        return(Tuple.Create(typeof(DBNullableTypeConverter <>).MakeGenericType(propertyType), interfaceType));
                    }
                }
                else if (property.IsSimpleType && property.PropertyType.IsEnum)
                {
                    return(Tuple.Create(typeof(EnumConverter <>).MakeGenericType(property.PropertyType), interfaceType));
                }
                else if (property.PropertyType == typeof(byte[]))
                {
                    return(Tuple.Create(typeof(DBNullableByteArrayConverter).MakeGenericType(property.PropertyType), interfaceType));
                }
            }
            return(typeConverter);
        }