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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ namespace System.IO
// whereas the Stream class is designed for byte input and output.
public class StreamReader : TextReader
{
public override async ValueTask DisposeAsync()
{
Dispose(true);
GC.SuppressFinalize(this);
await Task.CompletedTask;
}

// StreamReader.Null is threadsafe.
public static new readonly StreamReader Null = new NullStreamReader();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace System.IO
//
// This class is intended for character input, not bytes.
// There are methods on the Stream class for reading bytes.
public abstract partial class TextReader : MarshalByRefObject, IDisposable
public abstract partial class TextReader : MarshalByRefObject, IDisposable, IAsyncDisposable
{
// Create our own instance to avoid static field initialization order problems on Mono.
public static readonly TextReader Null = new StreamReader.NullStreamReader();
Expand All @@ -40,6 +40,13 @@ protected virtual void Dispose(bool disposing)
{
}

public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
}
}

// Returns the next available character without actually reading it from
// the input stream. The current position of the TextReader is not changed by
// this operation. The returned value is -1 if no further characters are
Expand Down
5 changes: 4 additions & 1 deletion src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10909,6 +10909,8 @@ public virtual void WriteByte(byte value) { }
}
public partial class StreamReader : System.IO.TextReader
{
public override System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }

public static readonly new System.IO.StreamReader Null;
public StreamReader(System.IO.Stream stream) { }
public StreamReader(System.IO.Stream stream, bool detectEncodingFromByteOrderMarks) { }
Expand Down Expand Up @@ -11046,11 +11048,12 @@ public override void WriteLine(System.Text.StringBuilder? value) { }
public override System.Threading.Tasks.Task WriteLineAsync(string? value) { throw null; }
public override System.Threading.Tasks.Task WriteLineAsync(System.Text.StringBuilder? value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public abstract partial class TextReader : System.MarshalByRefObject, System.IDisposable
public abstract partial class TextReader : System.MarshalByRefObject, System.IDisposable, System.IAsyncDisposable
{
public static readonly System.IO.TextReader Null;
protected TextReader() { }
public virtual void Close() { }
public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
public virtual int Peek() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
using System.Collections.Generic;
using System.Text;
using Xunit;
using System.Threading.Tasks;

namespace System.IO.Tests
{
public partial class StreamReaderTests : FileCleanupTestBase
{
[Fact]
public async Task DisposeAsync_ClosesStreamAsync()
{
var ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello World"));
var reader = new StreamReader(ms);
await reader.DisposeAsync();
Assert.Throws<ObjectDisposedException>(() => ms.ReadByte());
}
}
}


namespace System.IO.Tests
{
Expand Down
Loading