[英]Object can be disposed of more than once - Best solution
我在以下代码中收到警告CA2202(对象可以多次丢弃):
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var br = new BinaryReader(responseStream))
{
responseValue = br.ReadBytes(500000);
}
}
看起来,当调用BinaryReaders Dispose时会调用responseStreams Dispose。 BinaryReader是否始终调用Streams Dispose方法?
一种解决方案是直接初始化ResponseStream并让BinaryReader负责处理流(当然,如果BinaryReader在任何情况下都会处理流,那么它只能工作)
Stream responseStream = response.GetResponseStream();
if(responseStream != null)
using (var br = new BinaryReader(responseStream)) //Always disposes the response stream?
[...]
我可以使用try / finalize而不是外部using语句来得到这样的东西:
Stream responseStream = null;
try
{
responseStream = response.GetResponseStream();
if (responseStream != null)
using (var br = new BinaryReader(responseStream))
{
responseValue = br.ReadBytes(500000);
}
}
finally
{
if(stream != null)
stream.Dispose;
}
当BinaryReader始终处理流时,这不好看,也不必要。 有没有更好/首选的解决方案来解决这类问题?
如果您使用VS 2012和.NET 4.5,则可以创建不会关闭流的BinaryReader。 例如:
using(var reader = new BinaryReader(theStream, new UTF8Encoding(), true)
{
//...
}
如果您使用BinaryReader(Stream)构造函数,则新的UTF8Encoding是默认值,如果您不想使用UTF8Encoding,则可以使用其他内容。 真实表示“是的,让流保持打开”。
构造如:
public BinaryReader(
Stream input,
Encoding encoding,
bool leaveOpen
)
希望这会引导您找到您想要的东西。 干杯。
您实际上可以查看如何实现BinaryReader.Dispose
:
protected virtual void Dispose(bool disposing) {
if (disposing) {
Stream copyOfStream = m_stream;
m_stream = null;
if (copyOfStream != null && !m_leaveOpen)
copyOfStream.Close();
}
m_stream = null;
m_buffer = null;
m_decoder = null;
m_charBytes = null;
m_singleChar = null;
m_charBuffer = null;
}
public void Dispose()
{
Dispose(true);
}
现在,问题是,什么是m_leaveOpen
? 这是一个标志,说明是否应该处理基础流! 它默认设置为false
,这意味着将处理基础流:
public BinaryReader(Stream input) : this(input, new UTF8Encoding(), false) {
}
public BinaryReader(Stream input, Encoding encoding) : this(input, encoding, false) {
}
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) {
// (...)
m_leaveOpen = leaveOpen;
// (...)
}
因此,您可以跳过using
流周围的语句,因为它将被处理:
responseStream = response.GetResponseStream();
if (responseStream != null)
{
using (var br = new BinaryReader(responseStream))
{
responseValue = br.ReadBytes(500000);
}
}
要不就
using (var br = new BinaryReader(response.GetResponseStream()))
{
responseValue = br.ReadBytes(500000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.