如果我处置其底层 Stream,是否需要处置 XmlReader?

发布于 2024-09-15 08:38:49 字数 1380 浏览 4 评论 0原文

我有以下方法 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;
}

StreamReaderGetData 处置。这是否意味着我不必处置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聽兲甴掵 2024-09-22 08:38:49

最好的“经验法则”是:

如果某些东西实现了 IDisposable,请始终将其包装在 using() 块中,以确保任何非托管资源拥有的已正确处置。

依赖“某物”的当前实现来处置底层资源这一事实是危险的,将所有内容包装在 中不会有什么坏处使用,只是为了安全 =)

The best "rule of thumb" to work by is:

If something implements IDisposable, always wrap it in a using() 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 =)

堇色安年 2024-09-22 08:38:49

你是对的,你不必必须处置读者。但在给出的代码中,它也不会造成伤害。

我不会在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文