我今天正在阅读关于StreamWriter ,并且遇到了这个属性BaseStream 。
我正在寻找一个定义并找到了这个
“获取与后备存储接口的基础流。”
从这里MSDN - StreamWriter.BaseStream
我理解BaseStream对于StreamReader是什么,因为它的定义非常简单:
返回基础流。
但是StreamWriter.BaseStream的定义是什么意思? 或者更清楚的是,这部分定义的意思是“与后备存储的接口”? 这对我来说听起来像是胡言乱语。
I was reading about StreamWriter today, and came across this property, BaseStream.
I was looking for a definition and found this
"Gets the underlying stream that interfaces with a backing store."
from here MSDN - StreamWriter.BaseStream
I understand what a BaseStream is for StreamReader, because it's definition is very simply:
Returns the underlying stream.
But what does the definition of the StreamWriter.BaseStream mean?? Or more clearly, what does this part of the definition mean "interfaces with a backing store"? That sounds like gibberish to me.
最满意答案
你是对的; 它似乎不必要地冗长,特别是与类似的StreamReader.BaseStream 。 实际上,它只返回对底层流的引用,与StreamReader完全相同。
我认为描述的假设是写入底层流将涉及将写入的数据保存到某种持久性存储,例如文件。 当然,实际上这根本不是必需的(在最坏的情况下,它可能根本就什么都不做)。
如果您真的想要推断,您可以将其解释为底层流的CanWrite属性为true (至少在它附加到StreamWriter的位置)。
为了确信它真的只是返回底层流,这里是Reflector的反编译代码:
public virtual Stream BaseStream { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return this.stream; } }在Init方法中分配stream字段的位置:
private void Init(Stream stream, Encoding encoding, int bufferSize) { this.stream = stream; ...反过来由构造函数调用,参数是附加流:
[SecuritySafeCritical] public StreamWriter(Stream stream, Encoding encoding, int bufferSize) : base(null) { ... this.Init(stream, encoding, bufferSize); }You're right; it does seem unnecessarily wordy, especially in comparison with the analogous StreamReader.BaseStream. In reality, it just returns a reference to the underlying stream, exactly like StreamReader.
I think the presumption of the description is that writing to the underlying stream will involve saving the written data to some sort of persistent store, such as a file. Of course, this isn't necessary at all in reality (in the worst case, it could simply do nothing).
If you really wanted to extrapolate, you could interpret that as meaning that the underlying stream's CanWrite property is true (at least at the point it was attached to the StreamWriter).
To be convinced that it really is just returning the underlying stream, here's the decompiled code from Reflector:
public virtual Stream BaseStream { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return this.stream; } }where the stream field is assigned in the Init method:
private void Init(Stream stream, Encoding encoding, int bufferSize) { this.stream = stream; ...which in turn is called by the constructor with the argument being the attached stream:
[SecuritySafeCritical] public StreamWriter(Stream stream, Encoding encoding, int bufferSize) : base(null) { ... this.Init(stream, encoding, bufferSize); }更多推荐
发布评论