private static Func <object, object> GetConverter(StandardMapper 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); }
public Func <object, object> GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) { var t = targetProperty.PropertyType; if (typeof(IIdentity <int>).IsAssignableFrom(t)) { var ctor = t.GetConstructor(new Type[] { typeof(Int32) }); return((x) => ctor.Invoke(new object[] { (int)x })); } else if (typeof(IIdentity <Guid>).IsAssignableFrom(t)) { var ctor = t.GetConstructor(new Type[] { typeof(Guid) }); return((x) => ctor.Invoke(new object[] { (Guid)x })); } else { return(standardMapper.GetFromDbConverter(targetProperty, sourceType)); } }