当类具有IDisposable成员但没有非托管资源时,我应该实现IDisposable吗? [英] Should I implement IDisposable when class has IDisposable member but no unmanaged resources?

查看:75
本文介绍了当类具有IDisposable成员但没有非托管资源时,我应该实现IDisposable吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文档和此处关于StackOverflow的许多答案都竭尽全力地讨论了如何正确实现 IDisposable ,例如 MSDN IDisposable MSDN实施IDisposable 出色的StackOverflow问题与解答

The MSDN documentation and many answers here on StackOverflow go to lengths to disucss correctly implementing IDisposable, e.g. MSDN IDisposable, MSDN Implementing IDisposable, An excellent StackOverflow Q&A

但是没有其中的一个似乎涵盖了我遇到的一个更常见的用例:当我的类的 IDisposable 成员的寿命超过一个方法时该怎么办?例如,

However none of them seem to cover a more common use-case I have: what to do when my class has an IDisposable member that lives longer than one method? For example

  class FantasticFileService
  {
    private FileSystemWatcher fileWatch; // FileSystemWatcher is IDisposable

    public FantasticFileService(string path)
    {
      fileWatch = new FileSystemWatcher(path);
      fileWatch.Changed += OnFileChanged;
    }

    private void OnFileChanged(object sender, FileSystemEventArgs e)
    {
      // blah blah
    }
  }

最接近的 MSDN 致力于解决此问题,仅涵盖 IDisposable 是短暂的,所以说调用 Dispose 例如通过使用使用

The closest MSDN gets to addressing this problem only covers the use-case when the instance of IDisposable is short lived so says call Dispose e.g. by using using:


实施ID仅在使用非托管资源$ b时才可使用$ b直接。如果您的应用仅使用实现
IDisposable的对象,则不要提供IDisposable实现。相反,您
应该调用对象的IDisposable.Disable实现,当
完成使用它时。

Implement IDisposable only if you are using unmanaged resources directly. If your app simply uses an object that implements IDisposable, don't provide an IDisposable implementation. Instead, you should call the object's IDisposable.Dispose implementation when you are finished using it.

of当然,在这里我们需要实例比方法调用生存更长的时间是不可能的!?

of course that is not possible here where we need the instance to survive longer than a method call!?

我怀疑这样做的正确方法是实现 IDisposable (将责任交给了我班的创建者来处置),但没有所有终结器和受保护的虚拟void Dispose(布尔处置)逻辑上是因为我没有任何未管理的资源,即:

I suspect the correct way to do this would be to implement IDisposable (passing the responsibility to creator of my class to dispose it) but without all finalizer and protected virtual void Dispose(bool disposing) logic becuase I don't have any unmanged resources, i.e.:

  class FantasticFileService : IDisposable
  {
    private FileSystemWatcher fileWatch; // FileSystemWatcher is IDisposable

    public FantasticFileService(string watch)
    {
      fileWatch = new FileSystemWatcher(watch);
      fileWatch.Changed += OnFileChanged;
    }

    public void Dispose()
    {
      fileWatch.Dispose();
    }
  }

但是为什么这个用例未在有官方文件吗?而且它明确指出如果您的班级没有不受管理的资源,则不要实现 IDisposable 的事实使我犹豫不决……可怜的程序员该做什么? p>

But why is this use-case not explicitly covered in any official documentation? And the fact it explicitly says do not implement IDisposable if your class does not have unmanaged resources makes me hesitant to do so... What is a poor programmer to do?

推荐答案

看起来您的案例确实包含在某些文档中,即设计警告 CA1001:拥有一次性字段的类型应该是一次性的

It looks like your case is indeed covered by some documentation, namely the design warning CA1001: Types that own disposable fields should be disposable.

该链接提供了您的 IDisposable 实现的外观示例。它将如下所示。最终的设计指南可以在 CA1063:正确实现IDisposable 中找到。。 p>

That link has an example of what your IDisposable implementation should look like. It will be something like as follows. Eventual design guidelines can be found at CA1063: Implement IDisposable correctly.

  class FantasticFileService : IDisposable
  {
    private FileSystemWatcher fileWatch; // FileSystemWatcher is IDisposable

    public FantasticFileService(string watch)
    {
      fileWatch = new FileSystemWatcher(watch);
      fileWatch.Changed += OnFileChanged;
    }

    ~FantasticFileService()
    {
      Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
      if (disposing && fileWatch != null)
      {
        fileWatch.Dispose();
        fileWatch = null;
      }
    }

    public void Dispose()
    {
      Dispose(true);
      GC.SuppressFinalize(this);
    }
  }

这篇关于当类具有IDisposable成员但没有非托管资源时,我应该实现IDisposable吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆