public PocoData(Type t) { type = t; // Get the mapper for this type var mapper = Mappers.GetMapper(t); // Get the table info TableInfo = mapper.GetTableInfo(t); // Work out bound properties Columns = new Dictionary <string, PocoColumn>(ColumnComparer); foreach (var pi in t.GetProperties()) { 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.ComputedColumn = ci.ComputedColumn; pc.ForceToUtc = ci.ForceToUtc; // Store it Columns.Add(pc.ColumnName, pc); } // Build column list for automatic select QueryColumns = (from c in Columns where !c.Value.ResultColumn select c.Key).ToArray(); }
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)); } return(src => { var actualDstType = Nullable.GetUnderlyingType(dstType) ?? dstType; return Convert.ChangeType(src, actualDstType, null); }); } return(null); }
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); }