处置或不处置.. 作者通过导航器从 XmlDocument 创建的?
我真的需要处置下面的作者吗?
DataContractSerializer _serialier...
var actual = new XmlDocument();
using (var writer = actual.CreateNavigator().AppendChild())
_serialier.WriteObject(writer, myObj);
如果不是,则代码简化为:
DataContractSerializer _serialier...
var actual = new XmlDocument();
_serialier.WriteObject(actual.CreateNavigator().AppendChild(), myObj);
Do I really need to dispose the writer below?
DataContractSerializer _serialier...
var actual = new XmlDocument();
using (var writer = actual.CreateNavigator().AppendChild())
_serialier.WriteObject(writer, myObj);
If not then the code is simplified to:
DataContractSerializer _serialier...
var actual = new XmlDocument();
_serialier.WriteObject(actual.CreateNavigator().AppendChild(), myObj);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果该对象实现了 IDisposable,那么您应该在完成后对其调用 Dispose。
如果您不这样做,那么您的代码就依赖于您不需要这样做的假设。当您的代码稍后被重构以便使用的
XmlWriter
保留某些资源时,会发生什么情况?If the object implements IDisposable, then you should call Dispose on it when you're done.
If you don't do that, then your code is dependent on the assumption that you don't need to do it. What happens when your code is later refactored such that the
XmlWriter
being used is one that holds on to some resource?