如果我处置其底层 Stream,是否需要处置 XmlReader?
我有以下方法 GetData
从文件创建 StreamReader
。
private void GetData(string file)
{
string filename = Path.GetFileNameWithoutExtension(file);
XmlDocument xmldoc = new XmlDocument();
using (StreamReader sr = new StreamReader(file))
{
Stream bs = sr.BaseStream;
Stream cl = mainParser.CleanMarkup(bs);
try
{
xmldoc = mainParser.LoadDocument(bs);
}
catch (XmlException ex)
{
// Exceptions are usually caused by non-compliant documents.
// These errors are not critical to the operation of this program.
Console.WriteLine(filename + " " + ex.Message);
}
}
Msdn msdnParser = new Msdn(xmldoc);
ListViewItem lvitem = new ListViewItem(filename);
lvitem.SubItems.Add(filename);
foreach (string item in msdnParser.Subitems)
{
lvitem.SubItems.Add(item);
}
listView.Items.Add(lvitem);
}
mainParser.LoadDocument(bs)
调用以下内容:
public XmlDocument LoadDocument(Stream file)
{
XmlDocument xmldoc = new XmlDocument();
XmlReader xmlread = XmlReader.Create(file);
xmldoc.Load(xmlread);
return xmldoc;
}
StreamReader
由 GetData
处置。这是否意味着我不必处置 XmlReader
因为(我相信)这会处置其唯一的非托管资源?
I have the following method GetData
that creates a StreamReader
from a file.
private void GetData(string file)
{
string filename = Path.GetFileNameWithoutExtension(file);
XmlDocument xmldoc = new XmlDocument();
using (StreamReader sr = new StreamReader(file))
{
Stream bs = sr.BaseStream;
Stream cl = mainParser.CleanMarkup(bs);
try
{
xmldoc = mainParser.LoadDocument(bs);
}
catch (XmlException ex)
{
// Exceptions are usually caused by non-compliant documents.
// These errors are not critical to the operation of this program.
Console.WriteLine(filename + " " + ex.Message);
}
}
Msdn msdnParser = new Msdn(xmldoc);
ListViewItem lvitem = new ListViewItem(filename);
lvitem.SubItems.Add(filename);
foreach (string item in msdnParser.Subitems)
{
lvitem.SubItems.Add(item);
}
listView.Items.Add(lvitem);
}
mainParser.LoadDocument(bs)
calls the following:
public XmlDocument LoadDocument(Stream file)
{
XmlDocument xmldoc = new XmlDocument();
XmlReader xmlread = XmlReader.Create(file);
xmldoc.Load(xmlread);
return xmldoc;
}
StreamReader
is disposed by GetData
. Does this mean that I don't have to dispose of XmlReader
since (I believe) this would dispose of its only unmanaged resource?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的“经验法则”是:
如果某些东西实现了
IDisposable
,请始终将其包装在using()
块中,以确保任何非托管资源拥有的已正确处置。依赖“某物”的当前实现来处置底层资源这一事实是危险的,将所有内容包装在
中不会有什么坏处使用
,只是为了安全 =)The best "rule of thumb" to work by is:
If something implements
IDisposable
, always wrap it in ausing()
block to ensure that any unmanaged resources it owns are disposed of correctly.Relying on the fact that the current implementation of "something" disposes of an underlying resource is dangerous and it won't hurt to wrap everything in a
using
, just to be on the safe side =)你是对的,你不必必须处置读者。但在给出的代码中,它也不会造成伤害。
我不会在 LoadDocument() 中放置一个 using 块,因为它的设计是为了“借用”它的流(它不会创建它)。
但无论如何,还是有一些争论认为要处置 XmlReader,只是因为它是 IDisposable。我认为这里没有明显的赢家,因为 Reader(和 Writer)系列的设计存在争议:他们在没有明确成为这些流的所有者的情况下处置其 baseStreams。
You're right, you don't have to dispose the reader. But in the code given, it wouldn't hurt either.
I would not put a using block inside
LoadDocument()
because it is designed so that it 'borrows' it's stream (it does not create it).But there are arguments to Dispose the XmlReader anyway, just because it's IDisposable. I don't think there is a clear winner here because of the disputable design of the Reader (and Writer) family: They Dispose their baseStreams without clearly being the owner of those streams.