gpt4 book ai didi

c# - 同一 using block 中的多个变量

转载 作者:太空狗 更新时间:2023-10-29 23:51:49 25 4
gpt4 key购买 nike

<分区>

我目前正在使用如下两个对象:

using (var ms = new MemoryStream())
using (var bw = new BinaryWriter(ms))
{
// work with ms and bw, both referenced here
}

它工作“很好”,实际上是 an answer here也。但是,当我运行 VS2012 的代码分析工具时,我收到如下警告:

CA2202  Do not dispose objects multiple times   
Object 'ms' can be disposed more than once in method '<my method name>'.
To avoid generating a System.ObjectDisposedException you should not
call Dispose more than one time on an object.

这让我相信可能有一种替代方法来处理这种情况,但我不知道它是什么。

有谁知道在单个 using block 中以无警告方式使用两个对象的“正确”方法是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com