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.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(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); }); } else if (dstType == typeof(Guid) && srcType == typeof(string)) { return(delegate(object src) { return Guid.Parse((string)src); }); } else { return(delegate(object src) { return Convert.ChangeType(src, dstType, null); }); } } return(null); }