Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-spinelli committed Sep 22, 2022
1 parent a31dfd3 commit c1f8541
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 3 additions & 6 deletions Dapper/SqlMapper.IDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,11 @@ public static Func<IDataReader, T> GetRowParser<T>(this IDataReader reader, Type
{
concreteType ??= typeof(T);
var func = GetDeserializer(concreteType, reader, startIndex, length, returnNullIfFirstMissing);
if (concreteType.IsValueType)
{
if (func.Method.ReturnType != typeof(T))
{ // Always use a wrapper: this works for value and ref types (delegate casting is not an option).
return _ => (T)func(_);
}
else
{
return (Func<IDataReader, T>)(Delegate)func;
}
return (Func<IDataReader, T>)(Delegate)func;
}
}
}
15 changes: 15 additions & 0 deletions tests/Dapper.Tests/DataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ union all
Assert.Equal("qwe", bar.HazNameIdObject.Name);
}

/// <summary>
/// See https://github.com/DapperLib/Dapper/issues/1822
/// </summary>
[Fact]
public void issue_number_1822()
{
var reader = connection.ExecuteReader("select 'test' as col");
var rowParser = reader.GetRowParser<string>();

reader.Read();
// This should not throw!
string result = rowParser(reader);
Assert.Equal("test", result);
}

private abstract class Discriminated_BaseType
{
public abstract int Type { get; }
Expand Down

0 comments on commit c1f8541

Please sign in to comment.