diff --git a/Dapper NET40/SqlMapper.cs b/Dapper NET40/SqlMapper.cs index 01f726b5b..2da6be68a 100644 --- a/Dapper NET40/SqlMapper.cs +++ b/Dapper NET40/SqlMapper.cs @@ -898,7 +898,7 @@ internal static DbType LookupDbType(Type type, string name, bool demand, out ITy handler = null; var nullUnderlyingType = Nullable.GetUnderlyingType(type); if (nullUnderlyingType != null) type = nullUnderlyingType; - if (type.IsEnum() && !typeMap.ContainsKey(type)) + if (type.IsEnum() && !typeMap.ContainsKey(type) && !typeHandlers.ContainsKey(type)) { type = Enum.GetUnderlyingType(type); } @@ -3495,6 +3495,15 @@ private static Func GetStructDeserializer(Type type, Type e } #pragma warning restore 618 + ITypeHandler handler; + if(typeHandlers.TryGetValue(type, out handler)) + { + return r => + { + var val = r.GetValue(index); + return val is DBNull ? null : handler.Parse(type, val); + }; + } if (effectiveType.IsEnum()) { // assume the value is returned as the correct type (int/byte/etc), but box back to the typed enum return r => @@ -3507,15 +3516,6 @@ private static Func GetStructDeserializer(Type type, Type e return val is DBNull ? null : Enum.ToObject(effectiveType, val); }; } - ITypeHandler handler; - if(typeHandlers.TryGetValue(type, out handler)) - { - return r => - { - var val = r.GetValue(index); - return val is DBNull ? null : handler.Parse(type, val); - }; - } return r => { var val = r.GetValue(index); @@ -3529,6 +3529,11 @@ private static T Parse(object value) if (value is T) return (T)value; var type = typeof(T); type = Nullable.GetUnderlyingType(type) ?? type; + ITypeHandler handler; + if (typeHandlers.TryGetValue(type, out handler)) + { + return (T)handler.Parse(type, value); + } if (type.IsEnum()) { if (value is float || value is double || value is decimal) @@ -3537,11 +3542,6 @@ private static T Parse(object value) } return (T)Enum.ToObject(type, value); } - ITypeHandler handler; - if (typeHandlers.TryGetValue(type, out handler)) - { - return (T)handler.Parse(type, value); - } return (T)Convert.ChangeType(value, type, CultureInfo.InvariantCulture); }