帮助理解 .NET 中的读取器/写入器模式
以下是 XContainer.CreateWriter 方法的文档:
创建一个可用于将节点添加到 XContainer 的 XmlWriter。
我用心解析文本,我得到:
- 由类 XContainer 实现的方法
- 返回类 XmlWriter 的实例
- 目的是将节点添加到调用该方法的 XContainer 实例中。
然而,我看到XmlWriter,我就想到了“编写XML”。但这与我心中的上述目的相矛盾,即添加节点。如果我要添加节点,那么“阅读器”不是我用来添加节点的东西吗?
除了我这里的困惑之外,我没有声称有任何问题。我需要帮助来理解适当的比喻,以便我将来可以保持清晰。
读取器/写入器中使用的命名约定与 .NET 框架类中的功能有何关系?
Here's the documentation for the method XContainer.CreateWriter:
Creates an XmlWriter that can be used to add nodes to the XContainer.
I parse the text with my mind, I get:
- A method implemented by the class XContainer
- Returns an instance of the class XmlWriter
- The purpose is to add nodes to the instance of the XContainer on which the method is called.
However, I see XmlWriter, and I think of "writing XML". But that contradicts the above purpose in my mind, which is to add nodes. If I'm going to be adding nodes, wouldn't a "reader" be the thing I use to do so?
I'm not claiming anything is wrong other than my confusion here. I'd like assistance in grasping the apropriate metaphor so I can keep this straight in the future.
How is the naming convention used in Readers/Writers related to thier functionality in .NET framework classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读取不涉及对数据结构的修改。这就是为什么您拥有
只读访问权限
,这意味着无法进行任何修改。在您的情况下,您将修改数据结构(添加节点),因此您不仅要读取,还要写入(那些新节点)。
附言。你的第二个要点并不完全正确。该方法也可以返回从
XmlWriter
派生的类。Reading involves no modification to the data structure. That's why you have
read-only access
which means that no modification can be made.In your case you will be modifying the data structure (adding nodes) therefore you're not only reading but also writing (those new nodes).
PS. Your second bullet point is not entirely true. The method can return the class that derives from
XmlWriter
, too.