Commit 92644721 authored by Marc Gravell's avatar Marc Gravell

Be more defensive when adding SqlDataRecord handler; fix #424

parent a3ebdfdb
......@@ -24,6 +24,7 @@
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
......@@ -214,23 +215,35 @@ static SqlMapper()
[typeof(TimeSpan?)] = DbType.Time,
[typeof(object)] = DbType.Object
};
#if !COREFX
AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), false);
AddTypeHandlerImpl(typeof(IEnumerable<Microsoft.SqlServer.Server.SqlDataRecord>), new SqlDataRecordHandler(), false);
#endif
ResetTypeHandlers(false);
}
/// <summary>
/// Clear the registered type handlers
/// </summary>
public static void ResetTypeHandlers()
{
ResetTypeHandlers(true);
}
private static void ResetTypeHandlers(bool clone)
{
typeHandlers = new Dictionary<Type, ITypeHandler>();
#if !COREFX
AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), true);
AddTypeHandlerImpl(typeof(IEnumerable<Microsoft.SqlServer.Server.SqlDataRecord>), new SqlDataRecordHandler(), true);
AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), clone);
try // see https://github.com/StackExchange/dapper-dot-net/issues/424
{
AddSqlDataRecordsTypeHandler(clone);
} catch { }
#endif
}
#if !COREFX
[MethodImpl(MethodImplOptions.NoInlining)]
private static void AddSqlDataRecordsTypeHandler(bool clone)
{
AddTypeHandlerImpl(typeof(IEnumerable<Microsoft.SqlServer.Server.SqlDataRecord>), new SqlDataRecordHandler(), clone);
}
#endif
/// <summary>
/// Configure the specified type to be mapped to a given db-type
/// </summary>
......@@ -310,8 +323,8 @@ public static void AddTypeHandler<T>(TypeHandler<T> handler)
{
AddTypeHandlerImpl(typeof(T), handler, true);
}
private static Dictionary<Type, ITypeHandler> typeHandlers = new Dictionary<Type, ITypeHandler>();
private static Dictionary<Type, ITypeHandler> typeHandlers;
internal const string LinqBinary = "System.Data.Linq.Binary";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment