首页 > 解决方案 > 为什么用`count`参数0调用`StreamReader.ReadAsync`会推进底层流的`Position`?

问题描述

在使用System.IO.StreamReader.NET Framework 的类时,我注意到在我看来奇怪的不一致行为:

调用该方法Read(Char[], Int32, Int32)并传递参数(即读取零字节),底层对象的属性正如预期的那样。0countPositionStream0

然而,以同样的方式调用该ReadAsync(Char[], Int32, Int32)方法,Position底层Stream对象的属性并不像我预期的那样为零,而是流内容的长度;我希望它在同步测试用例中为零。

这是框架实现中的错误还是我遗漏了什么?

以下测试代码演示:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Threading.Tasks;

namespace ExplorationTesting
{

    [TestClass]
    public class TestStreamReader
    {

        public static Stream CreateStream(string fromString)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write(fromString);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }

        [TestMethod] // Test Passes
        public void TestReadingZeroBytesFromStreamReader_StreamPositionIsZero()
        {
            using (Stream stream = CreateStream("Foo"))
            using (StreamReader reader = new StreamReader(stream)) {
                Assert.AreEqual(0, stream.Position);
                char[] buffer = new char[] { };
                var r = reader.Read(buffer, 0, 0);
                Assert.AreEqual(0, r);
                Assert.AreEqual(0, stream.Position);
            }
        }

        [TestMethod] // Test Fails
        public async Task TestReadingZeroBytesFromStreamReader_StreamPositionIsZero_Async()
        {

            using (Stream stream = CreateStream("Foo"))
            using (StreamReader reader = new StreamReader(stream)) {
                Assert.AreEqual(0, stream.Position);
                char[] buffer = new char[] { };
                var r = await reader.ReadAsync(buffer, 0, 0);
                Assert.AreEqual(0, r);
                Assert.AreEqual(0, stream.Position); // Assert.AreEqual failed. Expected:<0>. Actual:<3>. 
            }
        }
    }
}

标签: c#.netstreamreaderdefects

解决方案


推荐阅读