c# - .Net StreamWriter.BaseStream,这个定义是什么意思? "Gets the underlying stream that interfaces with a backing store."

标签 c# .net stream streamwriter

我今天在阅读有关 StreamWriter 的内容时,偶然发现了这个属性 BaseStream

我正在寻找一个定义并找到了这个

"Gets the underlying stream that interfaces with a backing store."

从这里MSDN - StreamWriter.BaseStream

我理解 StreamReader 的 BaseStream 是什么,因为它的定义非常简单:

Returns the underlying stream.

但是 StreamWriter.BaseStream 的定义是什么意思??或者更清楚地说,定义的这一部分是什么意思“与后备存储的接口(interface)”?这对我来说听起来像是胡言乱语。

最佳答案

你是对的;它确实显得过于冗长,尤其是与类似的 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);
}

关于c# - .Net StreamWriter.BaseStream,这个定义是什么意思? "Gets the underlying stream that interfaces with a backing store.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4653543/

相关文章:

c# - 资源服务器如何从 token 中识别用户?

c# - 检测事件是否是从另一个类设置的

c# - 抽象类设计 : Why not define public constructores?

输入流在对象内结束

java - 当我将 I/O 流包装两次时会发生什么?

firebase - Flutter 使用 Stream 还是 Future?

c# - Aes 加密不起作用

c# - 尝试添加叠加层以映射错误

c# - 使用 MSTest 测试异步 WCF 服务

.net - JSON.NET 反序列化