使用语句和流

发布于 2024-11-23 15:02:14 字数 1152 浏览 3 评论 0原文

我真的很困惑下面发生的事情。如果我只是尝试返回在 using 语句中创建的 MemoryStream,它将不起作用。但是,如果我调用 ToArray 并创建另一个流,效果会很好。我也尝试从 using 语句中取出第一个 MemoryStream,但结果是相同的。

        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (StreamWriter streamWriter = new StreamWriter(memoryStream))
            {
                using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter))
                {
                    JsonSerializer jsonSerializer = new JsonSerializer();
                    jsonSerializer.Converters.Add(new JavaScriptDateTimeConverter());
                    jsonSerializer.NullValueHandling = NullValueHandling.Ignore;
                    jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    jsonSerializer.PreserveReferencesHandling = PreserveReferencesHandling.None;
                    jsonSerializer.Serialize(jsonWriter, account);

                    streamWriter.Flush();
                }
            }

            // Works
            return new MemoryStream(memoryStream.ToArray());
            // Doesn't work
            return memoryStream;
        }

I'm really confused as to what's going on below. If I just try and return the MemoryStream that is created in the using statement it doesn't work. However, if I call ToArray and create another stream it works great. I have alo tried taking the first MemoryStream out of the using statement but the result is the same.

        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (StreamWriter streamWriter = new StreamWriter(memoryStream))
            {
                using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter))
                {
                    JsonSerializer jsonSerializer = new JsonSerializer();
                    jsonSerializer.Converters.Add(new JavaScriptDateTimeConverter());
                    jsonSerializer.NullValueHandling = NullValueHandling.Ignore;
                    jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    jsonSerializer.PreserveReferencesHandling = PreserveReferencesHandling.None;
                    jsonSerializer.Serialize(jsonWriter, account);

                    streamWriter.Flush();
                }
            }

            // Works
            return new MemoryStream(memoryStream.ToArray());
            // Doesn't work
            return memoryStream;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-11-30 15:02:14
  1. using 处理流。
  2. 看来 StreamWriter.Dispose() 处理了底层流。因此,要么找到 StreamWriter 的任何替代品,要么像您一样制作副本。
  1. using disposes the stream.
  2. It appears that StreamWriter.Dispose() disposes underlying stream. So either find any alternative to StreamWriter that doesn't or make a copy like you did.
递刀给你 2024-11-30 15:02:14

“using”在关闭时会执行隐式的 .Dispose() 操作,因此我希望它不会与您想要的效果很好地配合。

我希望不使用内存流的“使用”确实会起作用,好奇为什么这对你不起作用。

"using" does an implicit .Dispose() when it closes, so I'd expect that to not play well with what you're trying to.

I would expect that not using the "using" for the memoryStream would indeed work, curious why that didn't work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文