Esempio 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);
        }
Esempio 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(delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); });
            }

            if (srcType == typeof(TimeSpan) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((TimeSpan)src).Ticks); });
            }

            if (srcType == typeof(UInt64) && (dstType == typeof(bool) || dstType == typeof(bool?)))
            {
                //if (null != Nullable.GetUnderlyingType(dstType))
                //{
                //    var convert = new NullableConverter(dstType);
                //    return delegate (object src) { return convert.ConvertFrom(src + ""); };
                //}
                return(delegate(object src) { return src.ToBool_(); });
            }

            // Forced type conversion including integral types -> enum
            if ((dstType.IsEnum || (null != Nullable.GetUnderlyingType(dstType) && Nullable.GetUnderlyingType(dstType).IsEnum)) && IsIntegralType(srcType))
            {
                //if (srcType != typeof(int))
                //{
                //    return delegate (object src) { return Convert.ChangeType(src, typeof(int), null); };
                //}
                if (null != Nullable.GetUnderlyingType(dstType))
                {
                    var convert = new NullableConverter(dstType);
                    return(delegate(object src) { return convert.ConvertFrom(src + ""); });
                }
                else
                {
                    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, src + ""); });
                }
                else if (null != Nullable.GetUnderlyingType(dstType))
                {
                    var convert = new NullableConverter(dstType);
                    return(delegate(object src) { return convert.ConvertFrom(src + ""); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
            }

            return(null);
        }