.Net StreamWriter.BaseStream,这个定义是什么意思?(.Net StreamWriter.BaseStream, what does this definition mean

编程入门 行业动态 更新时间:2024-10-25 12:27:08
.Net StreamWriter.BaseStream,这个定义是什么意思?(.Net StreamWriter.BaseStream, what does this definition mean? “Gets the underlying stream that interfaces with a backing store.”)

我今天正在阅读关于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); }

更多推荐

本文发布于:2023-08-07 09:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   definition   BaseStream   Net   StreamWriter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!