示例#1
0
        private static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            try
            {
                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); });
                }

                // unwrap nullable types
                Type underlyingDstType = Nullable.GetUnderlyingType(dstType);
                if (underlyingDstType != null)
                {
                    dstType = underlyingDstType;
                }

                // Forced type conversion including integral types -> enum
                if (dstType.IsEnum && IsIntegralType(srcType))
                {
                    var backingDstType = Enum.GetUnderlyingType(dstType);
                    if (underlyingDstType != null)
                    {
                        // if dstType is Nullable<Enum>, convert to enum value
                        return(delegate(object src) { return Enum.ToObject(dstType, src); });
                    }
                    else if (srcType != backingDstType)
                    {
                        return(delegate(object src) { return Convert.ChangeType(src, backingDstType, null); });
                    }
                }
                else if (!dstType.IsAssignableFrom(srcType))
                {
                    if (dstType.IsEnum && srcType == typeof(string))
                    {
                        return(delegate(object src) { return EnumMapper.EnumFromString(dstType, (string)src); });
                    }
                    if (dstType == typeof(Guid) && srcType == typeof(string))
                    {
                        return(delegate(object src) { return Guid.Parse((string)src); });
                    }
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
                return(null);
            }
            catch
            {
                throw;
            }
        }
示例#2
0
        public PocoData(Type type, IMapper defaultMapper)
        {
            Type = type;

            // Get the mapper for this type
            var mapper = Mappers.GetMapper(type, defaultMapper);

            // Get the table info
            TableInfo = mapper.GetTableInfo(type);

            // Work out bound properties
            Columns = new Dictionary <string, PocoColumn> (StringComparer.OrdinalIgnoreCase);
            foreach (var pi in type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ColumnInfo ci = mapper.GetColumnInfo(pi);
                if (ci == null)
                {
                    continue;
                }

                var pc = new PocoColumn();
                pc.PropertyInfo             = pi;
                pc.ColumnName               = ci.ColumnName;
                pc.ResultColumn             = ci.ResultColumn;
                pc.AutoSelectedResultColumn = ci.AutoSelectedResultColumn;
                pc.ForceToUtc               = ci.ForceToUtc;
                pc.InsertTemplate           = ci.InsertTemplate;
                pc.UpdateTemplate           = ci.UpdateTemplate;

                // Store it
                Columns.Add(pc.ColumnName, pc);
            }

            // Build column list for automatic select
            QueryColumns = (from c in Columns where !c.Value.ResultColumn || c.Value.AutoSelectedResultColumn select c.Key).ToArray();
        }