Exemplo n.º 1
0
        private static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (pc != null)
            {
                converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                if (converter != null)
                {
                    return(converter);
                }
            }

            // Standard DateTime->Utc mapper
            if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); });
            }

            // Forced type conversion including integral types -> enum
            if (dstType.IsEnum && IsIntegralType(srcType))
            {
                if (srcType != typeof(int))
                {
                    return(delegate(object src) { return Convert.ChangeType(src, typeof(int), null); });
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                if (dstType.IsEnum && srcType == typeof(string))
                {
                    return(delegate(object src) { return EnumMapper.EnumFromString(dstType, (string)src); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        private static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (pc != null)
            {
                converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                if (converter != null)
                {
                    return(converter);
                }
            }

            // Standard DateTime->Utc mapper
            if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(src => new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc));
            }

            // Forced type conversion including integral types -> enum
            if (IsEnumType(dstType) && IsIntegralType(srcType))
            {
                var fromInt    = (srcType == typeof(int));
                var toNullable = !dstType.IsEnum;

                if (fromInt && toNullable)
                {
                    return(src => EnumMapper.EnumFromNullableInt(dstType, (int?)src));
                }
                else if (toNullable)
                {
                    return(src => {
                        var i = Convert.ChangeType(src, typeof(int), null);
                        return EnumMapper.EnumFromNullableInt(dstType, i as int?);
                    });
                }
                else if (!fromInt)
                {
                    return(src => Convert.ChangeType(src, typeof(int), null));
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                if (IsEnumType(dstType) && srcType == typeof(string))
                {
                    return(src => EnumMapper.EnumFromString(dstType, (string)src));
                }
                else
                {
                    return(src =>
                    {
                        if (dstType == typeof(SqlGeography))
                        {
                            return src == null ? default(SqlGeography) : SqlGeography.Parse(new SqlString(src.ToString()));
                        }
                        return Convert.ChangeType(src, dstType, null);
                    });
                }
            }

            return(null);
        }