Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/CsvHelper/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -891,11 +891,12 @@ record = read();
}
}

/// <inheritdoc/>
public virtual IEnumerable<T> GetRecords<T>(T anonymousTypeDefinition)
{
if (anonymousTypeDefinition == null)
{
/// <inheritdoc/>
/// <exception cref="ObjectDisposedException">Thrown when the reader has been disposed.</exception>
public virtual IEnumerable<T> GetRecords<T>(T anonymousTypeDefinition)
{
if (anonymousTypeDefinition == null)
{
throw new ArgumentNullException(nameof(anonymousTypeDefinition));
}

Expand All @@ -907,11 +908,12 @@ public virtual IEnumerable<T> GetRecords<T>(T anonymousTypeDefinition)
return GetRecords<T>();
}

/// <inheritdoc/>
public virtual IEnumerable<object> GetRecords(Type type)
{
if (disposed)
{
/// <inheritdoc/>
/// <exception cref="ObjectDisposedException">Thrown when the reader has been disposed.</exception>
public virtual IEnumerable<object> GetRecords(Type type)
{
if (disposed)
{
throw new ObjectDisposedException(nameof(CsvReader),
"GetRecords<object>() returns an IEnumerable<T> that yields records. This means that the method isn't actually called until " +
"you try and access the values. e.g. .ToList() Did you create CsvReader inside a using block and are now trying to access " +
Expand Down
19 changes: 19 additions & 0 deletions tests/CsvHelper.Tests/Reading/YieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ public void GetRecords_Disposed_ThrowsObjectDisposedExceptionTest()
Assert.Throws<ObjectDisposedException>(() => records.ToList());
}

[Fact]
public void GetRecordsAnonymousType_Disposed_ThrowsObjectDisposedExceptionTest()
{
var parserMock = new ParserMock
{
new[] { "Id", "Name" },
new[] { "1", "one" },
null
};

IEnumerable<object> records;
using (var csv = new CsvReader(parserMock))
{
records = csv.GetRecords(new { Id = 0, Name = string.Empty }).Cast<object>();
}

Assert.Throws<ObjectDisposedException>(() => records.ToList());
}

[Fact]
public void EnumerateRecords_Disposed_ThrowsObjectDisposedExceptionTest()
{
Expand Down