diff --git a/Dapper NET40/SqlMapper.cs b/Dapper NET40/SqlMapper.cs index a7efe556c..69e64b295 100644 --- a/Dapper NET40/SqlMapper.cs +++ b/Dapper NET40/SqlMapper.cs @@ -699,6 +699,8 @@ where pair.Value > 1 static SqlMapper() { + SetDefaultTypeMap(t => new DefaultTypeMap(t)); + typeMap = new Dictionary(); typeMap[typeof(byte)] = DbType.Byte; typeMap[typeof(sbyte)] = DbType.SByte; @@ -3554,7 +3556,7 @@ static readonly MethodInfo getItem = typeof(IDataRecord).GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p => p.GetIndexParameters().Any() && p.GetIndexParameters()[0].ParameterType == typeof(int)) .Select(p => p.GetGetMethod()).First(); - + /// /// Gets type-map for the given type /// @@ -3580,7 +3582,7 @@ public static ITypeMap GetTypeMap(Type type) if (map == null) { - map = new DefaultTypeMap(type); + map = _defaultTypeMapFactory(type); _typeMaps[type] = map; } } @@ -3588,6 +3590,17 @@ public static ITypeMap GetTypeMap(Type type) return map; } + private static Func _defaultTypeMapFactory; + + /// + /// Set a custom mapping for all types + /// + /// A function that creates the ITypeMap + public static void SetDefaultTypeMap(Func factory) + { + _defaultTypeMapFactory = factory; + } + // use Hashtable to get free lockless reading #if DNXCORE50 private static readonly Dictionary _typeMaps = new Dictionary(); @@ -3604,8 +3617,8 @@ public static void SetTypeMap(Type type, ITypeMap map) { if (type == null) throw new ArgumentNullException("type"); - - if (map == null || map is DefaultTypeMap) + + if (map == null) { lock (_typeMaps) { @@ -5335,7 +5348,7 @@ public ParameterInfo Parameter /// /// Represents default type mapping strategy used by Dapper /// - sealed partial class DefaultTypeMap : SqlMapper.ITypeMap + partial class DefaultTypeMap : SqlMapper.ITypeMap { private readonly List _fields; private readonly List _properties; @@ -5404,7 +5417,7 @@ internal static List GetSettableFields(Type t) /// DataReader column names /// DataReader column types /// Matching constructor or default one - public ConstructorInfo FindConstructor(string[] names, Type[] types) + public virtual ConstructorInfo FindConstructor(string[] names, Type[] types) { var constructors = _type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (ConstructorInfo ctor in constructors.OrderBy(c => c.IsPublic ? 0 : (c.IsPrivate ? 2 : 1)).ThenBy(c => c.GetParameters().Length)) @@ -5466,7 +5479,7 @@ public ConstructorInfo FindExplicitConstructor() /// Constructor to resolve /// DataReader column name /// Mapping implementation - public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName) + public virtual SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName) { var parameters = constructor.GetParameters(); @@ -5478,7 +5491,7 @@ public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, /// /// DataReader column name /// Mapping implementation - public SqlMapper.IMemberMap GetMember(string columnName) + public virtual SqlMapper.IMemberMap GetMember(string columnName) { var property = _properties.FirstOrDefault(p => string.Equals(p.Name, columnName, StringComparison.Ordinal)) ?? _properties.FirstOrDefault(p => string.Equals(p.Name, columnName, StringComparison.OrdinalIgnoreCase));