我有以下代码(作为示例):
using(Stream s = new FileStream(path))
using(GZipStream gz = new GZipStream(s, CompressionMode.Compress))
{
//do stuff here
}
我在这里收到CA2202 "Do not dispose objects multiple times"
错误。这只是因为我没有在leaveOpen
中使用GZipStream
参数吗?
我在其他地方使用一个using语句得到了类似的错误,但显然是多次调用Dispose()
。
是否与this question或其他问题相同?
答案 0 :(得分:4)
因为GZipStream
也处理了Stream
。修复是要么在构造函数上使用leaveOpen
参数(在这种情况下它没有多大意义,因为你真的希望它仍然关闭)或者组合语句:
using(var gz = new GZipStream(new FileStream(path), CompressionMode.Compress))
{
// Do Stuff Here
}