我在实现IDisposable接口是否正确? [英] Am I implementing IDisposable correctly?

查看:96
本文介绍了我在实现IDisposable接口是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本类使用的StreamWriter ,因此农具的IDisposable

public class Foo : IDisposable
{
    private StreamWriter _Writer;

    public Foo (String path)
    {
        // here happens something along the lines of:
        FileStream fileWrite = File.Open (path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
        _Writer = new StreamWriter (fileWrite, new ASCIIEncoding ());
    }

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

    ~Foo()
    {
        Dispose (false);
    }

    protected virtual void Dispose (bool disposing)
    {
        if (_Disposed) {
            return;
        }
        if (disposing) {
            _Writer.Dispose ();
        }
        _Writer = null;
        _Disposed = true;
    }
    private bool _Disposed;
}

}

是否与目前执行的任何问题?即,我必须手动基本的FileStream 释放?为的Dispose(布尔)写的是否正确?

Is there any issue with the current implementation? I.e., do I have to release the underlying FileStream manually? Is Dispose(bool) written correctly?

推荐答案

您不必使用这种粗放IDisposable的实现版本,如果你的类不直接使用非托管资源。

You don't need to use this extensive version of IDisposable implementation if your class doesn't directly use unmanaged resources.

一个简单的

 public virtual void Dispose()
 {

     _Writer.Dispose();
 }

就足够了。

如果您的消费者未处置你的对象时,它通常没有调用Dispose被GC'd,由_writer举行的对象也将GC'd,它将有一个finaliser所以它仍然得到清理其非托管资源正常。

If your consumer fails to Dispose your object it will be GC'd normally without a call to Dispose, the object held by _Writer will also be GC'd and it will have a finaliser so it still gets to clean up its unmanaged resources properly.

修改

做完上由Matt提供的链接和其他一些研究,我得出的结论是,我的答案在这里的表示的。这是为什么: -

Having done some research on the links provided by Matt and others I've come to the conclusion that my answer here stands. Here is why:-

一次性执行模式背后的premise(我的意思是受保护的虚拟的Dispose(布尔),燮pressFinalize等marlarky)上的可继承类是一个子类的可能扶住非托管资源。

The premise behind the disposable implementation "pattern" (by that I mean the protected virtual Dispose(bool), SuppressFinalize etc. marlarky) on an inheritable class is that a sub-class might hold on to an unmanaged resource.

然而,在现实世界中的我们绝大多数.NET开发人员永远不会去任何地方附近的非托管资源。如果你有量化的可能上面,你会想出什么probabilty人物与你之类的.NET编码?

However in the real world the vast majority of us .NET developers never go anywhere near an unmanaged resource. If you had to quantify the "might" above what probabilty figure would you come up with for you sort of .NET coding?

让我们假设我有一个Person类型(为了便于讨论在其领域中的一个具有一次性的,因此应该是一次性的本身)。现在我有传承客户,员工等是否真的合理,我弄乱这个模式,以防万一有人继承人,并希望持有非托管资源Person类?

Lets assume I have a Person type (which for sake of argument has a disposable type in one of its fields and hence ought to be disposable itself). Now I have inheritors Customer, Employee etc. Is it really reasonable for me to clutter the Person class with this "Pattern" just in case someone inherits Person and wants to hold an unmanaged resource?

有时,我们的开发人员可以在东西企图code所有可能的情况下,不使用对这样的情况的相对概率的一些常识复杂化。

Sometimes we developers can over complicate things in an attempt to code for all possible circumstances without using some common sense regarding the relative probability of such circumstances.

如果我们曾经想使用非托管资源的直接模式懂事将包装在自己的类这样的事情,其中​​完整的一次性模式将是合理的。因此,在显著庞大的身躯正常code,我们并没有把约所有瞎担心。如果我们需要IDisposable接口,我们可以使用上面,可继承与否简单的模式。

If we ever wanted to use an unmanaged resource directly the sensible pattern would be wrap such a thing in its own class where the full "disposable pattern" would be reasonable. Hence in the significantly large body of "normal" code we do not to have to worry about all that mucking about. If we need IDisposable we can use the simple pattern above, inheritable or not.

唷,很高兴得到了我的胸口。 ;)

Phew, glad to get that off my chest. ;)

这篇关于我在实现IDisposable接口是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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