是否确定有一个虚拟的Dispose()方法,只要层次结构中的所有类只使用管理的资源? [英] Is it OK to have a virtual Dispose() method as long as all classes in the hierarchy only use managed resources?

查看:157
本文介绍了是否确定有一个虚拟的Dispose()方法,只要层次结构中的所有类只使用管理的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的一般准则实施的 Dispose模式警告说,不要实现虚拟的Dispose()。然而,最常见的我们只在与托管资源类里面等等全Dispose模式似乎是一种矫枉过正 - 也就是说,我们并不需要一个终结。在这种情况下,它是确定有一个虚拟的的Dispose()在基类?

I know that the general guideline for implementing the dispose pattern warns against implementing a virtual Dispose(). However, most often we're only working with managed resources inside a class and so the full dispose pattern seems like an overkill - i.e. we don't need a finalizer. In such cases, is it OK to have a virtual Dispose() in the base class?

考虑一个简单的例子:

abstract class Base : IDisposable
{
    private bool disposed = false;
    public SecureString Password { get; set; } // SecureString implements IDisposable.

    public virtual void Dispose()
    {
        if (disposed)
            return;

        if (Password != null)       
            Password.Dispose();     

        disposed = true;
    }
}

class Sub : Base
{
    private bool disposed = false;
    public NetworkStream NStream { get; set; } // NetworkStream implements IDisposable.

    public override void Dispose()
    {   
        if (disposed)
            return;

        if (NStream != null)
            NStream.Dispose();

        disposed = true;
        base.Dispose();
    }
}

我觉得这是更具可读性比一个完整的Dispose模式。据我所知,这个例子会成为问题,如果我们引入非托管资源到基本类。但是,假如这不会发生。是上面的例子则有效/安全/健壮,还是它带来任何问题吗?我应该坚持的标准完全成熟的Dispose模式,即使没有非托管资源的使用?还是上面的办法确实是在这种情况下完全确定 - 这在我的经验是更为常见比处理非托管资源

I find this more readable than a complete dispose pattern. I understand that this example would become problematic if we introduced an unmanaged resource into the Base class. But suppose that this won't happen. Is the above example then valid/safe/robust, or does it pose any problem? Should I stick to the standard full-blown dispose pattern, even if no unmanaged resources are used? Or is the above approach indeed perfectly OK in this situation - which in my experience is far more common than dealing with unmanaged resources?

推荐答案

我已经放弃了全面的的IDisposable 模式的情况下,我不处理非托管资源。

I have given up on the full-blown IDisposable pattern in cases where I am not dealing with unmanaged resources.

我看不出有任何理由你不应该能够放弃的格局,使处置虚拟如果可以保证派生类不会引入非托管资源!

I don't see any reason why you shouldn't be able to forego the pattern and make Dispose virtual if you can ensure that derived classes won't introduce unmanaged resources!

(而这就是一个问题,因为你的不能的实施这一点。你在什么字段派生类将增加无法控制,所以没有绝对的安全性与虚拟处置。但后来,即使你使用了完全成熟的模式,派生类可以把事情搞错了,所以总有一些信托涉及的亚型遵守一定的规则。)

(And here lies a problem, because you cannot enforce this. You have no control over what fields a derived class will add, so there is no absolute safety with a virtual Dispose. But then even if you used the full-blown pattern, a derived class could get things wrong, so there is always some trust involved that subtypes adhere to certain rules.)

首先,在情况下,我们只处理管理对象,有一个终结将是毫无意义的:如果处置从终结所谓的(处置(处置:FALSE)根据全面爆发模式),我们可能无法安全地访问/解除引用其它参考类型的管理的对象,因为他们可能已经走了。唯一的价值类型对象被最终确定可以安全地访问,对象访问

First, in cases where we only deal with managed objects, having a finalizer would be pointless: If Dispose is called from the finalizer (Dispose(disposing: false) according to the full-blown pattern), we may not safely access/dereference other reference-typed managed objects because they might already be gone. Only value-typed objects reachable from the object being finalized may be safely accessed.

如果终结是没有意义的,也是没有意义的有处理标记,用于区分是否处置被显式调用,或者是否被调用的终结。

If the finalizer is pointless, it is also pointless to have the disposing flag, which is used to distinguish whether Dispose was explicitly called or whether it was called by the finalizer.

这也是没有必要做 GC.Sup pressFinalize 如果没有终结。

It is also unnecessary to do GC.SuppressFinalize if there is no finalizer.

<打击>我甚至不知道是否它是那么仍然势在必行处置不是扔在一个托管只场景任何异常。 AFAIK这条规则的存在主要是为了保护终结器线程。(见@ USR的<一个href="http://stackoverflow.com/questions/32354386/is-it-ok-to-have-a-virtual-dispose-method-as-long-as-all-classes-in-the-hierar/32354935?noredirect=1#comment52582924_32354935">comment下面。)

I am not even sure whether it is then still imperative that Dispose not throw any exceptions in a managed-only scenario. AFAIK this rule exists mostly to protect the finalizer thread. (See @usr's comment below.)

正如你所看到的,一旦终结的推移,很多经典的一次性模式不再是必要的。

As you can see, once the finalizer goes, much of the classic Disposable pattern is no longer necessary.

这篇关于是否确定有一个虚拟的Dispose()方法,只要层次结构中的所有类只使用管理的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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