本文介绍了BinaryReader.Dispose(BOOL处置)创建一个本地引用流。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了整箱的代码不同寻常的样品



这是在System.IO.BinaryReader方式:

 受保护的虚拟无效的Dispose(BOOL处置){
如果(处置){
流copyOfStream = m_stream;
m_stream = NULL;
如果(copyOfStream = NULL&放大器;!&安培;!m_leaveOpen)
copyOfStream.Close();
}
m_stream = NULL;
m_buffer = NULL;
m_decoder = NULL;
m_charBytes = NULL;
m_singleChar = NULL;
m_charBuffer = NULL;
}

在执行逻辑有什么影响copyOfStream?


解决方案

它的完成使m_stream将被设置为null即使 Stream.Close()抛出异常。



在回应评论



When you close a stream, any buffered output is flushed, which can throw an exception, e.g. due to a network failure.

In the above, if Stream.Close() throws, then m_stream will already be null. With the following alternative, m_stream won't be set to null if Stream.Close() throws:

m_stream.Close(); // <== throws
m_stream = null;  // <== not executed if Close() throws

It won't throw a NullReferenceException because it tests for null:

if (copyOfStream != null && !m_leaveOpen)
        copyOfStream.Close();

这篇关于BinaryReader.Dispose(BOOL处置)创建一个本地引用流。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:03