Commit e1b503a8 authored by mgravell's avatar mgravell

detect IEnumerable<T> for some T : IDataRecord dynamically, rather than via a...

detect IEnumerable<T> for some T : IDataRecord dynamically, rather than via a hard-coded handler (AddSqlDataRecordsTypeHandler)
parent 32b536a1
...@@ -224,22 +224,11 @@ private static void ResetTypeHandlers(bool clone) ...@@ -224,22 +224,11 @@ private static void ResetTypeHandlers(bool clone)
#if !NETSTANDARD1_3 #if !NETSTANDARD1_3
AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), clone); AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), clone);
#endif #endif
try
{
AddSqlDataRecordsTypeHandler(clone);
}
catch { /* https://github.com/StackExchange/dapper-dot-net/issues/424 */ }
AddTypeHandlerImpl(typeof(XmlDocument), new XmlDocumentHandler(), clone); AddTypeHandlerImpl(typeof(XmlDocument), new XmlDocumentHandler(), clone);
AddTypeHandlerImpl(typeof(XDocument), new XDocumentHandler(), clone); AddTypeHandlerImpl(typeof(XDocument), new XDocumentHandler(), clone);
AddTypeHandlerImpl(typeof(XElement), new XElementHandler(), clone); AddTypeHandlerImpl(typeof(XElement), new XElementHandler(), clone);
} }
[MethodImpl(MethodImplOptions.NoInlining)]
private static void AddSqlDataRecordsTypeHandler(bool clone)
{
AddTypeHandlerImpl(typeof(IEnumerable<Microsoft.SqlServer.Server.SqlDataRecord>), new SqlDataRecordHandler<Microsoft.SqlServer.Server.SqlDataRecord>(), clone);
}
/// <summary> /// <summary>
/// Configure the specified type to be mapped to a given db-type. /// Configure the specified type to be mapped to a given db-type.
/// </summary> /// </summary>
...@@ -396,6 +385,29 @@ public static DbType LookupDbType(Type type, string name, bool demand, out IType ...@@ -396,6 +385,29 @@ public static DbType LookupDbType(Type type, string name, bool demand, out IType
} }
if (typeof(IEnumerable).IsAssignableFrom(type)) if (typeof(IEnumerable).IsAssignableFrom(type))
{ {
#if !NETSTANDARD1_3
// auto-detect things like IEnumerable<SqlDataRecord> as a family
if (type.IsInterface && type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(IEnumerable<>)
&& typeof(IEnumerable<IDataRecord>).IsAssignableFrom(type))
{
var argTypes = type.GetGenericArguments();
if(typeof(IDataRecord).IsAssignableFrom(argTypes[0]))
{
try
{
handler = (ITypeHandler)Activator.CreateInstance(
typeof(SqlDataRecordHandler<>).MakeGenericType(argTypes));
AddTypeHandlerImpl(type, handler, true);
return DbType.Object;
}
catch
{
handler = null;
}
}
}
#endif
return DynamicParameters.EnumerableMultiParameter; return DynamicParameters.EnumerableMultiParameter;
} }
......
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