在一个子类实现IDisposable的时候,家长也实现IDisposable [英] Implementing IDisposable on a subclass when the parent also implements IDisposable

查看:181
本文介绍了在一个子类实现IDisposable的时候,家长也实现IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父和子类都需要实施的IDisposable 。应该在哪里虚拟(和 base.Dispose()?)调用发挥作用?当我刚刚重写的Dispose(BOOL处置)通话,那感觉真是奇怪,说明我实施的IDisposable ,而无需一个明确的Dispose()函数(只是利用继承的一个),但有一切。

我已经做(小瞧了不少):

 内部类FooBase:IDisposable的
{
    插座baseSocket;

    私人无效SendNormalShutdown(){}

    公共无效的Dispose()
    {
        处置(真);
        GC.Sup pressFinalize(本);
    }

    私人布尔_disposed = FALSE;
    受保护的虚拟无效的Dispose(BOOL处置)
    {
        如果(!_disposed)
        {
            如果(处置)
            {
                SendNormalShutdown();
            }
            baseSocket.Close();
        }
    }

    〜FooBase()
    {
        处置(假);
    }
}

内部类Foo:FooBase,IDisposable的
{
    插座extraSocket;

    私人布尔_disposed = FALSE;
    保护覆盖无效的Dispose(BOOL处置)
    {
        如果(!_disposed)
        {
            extraSocket.Close();
        }
        base.Dispose(处置);
    }

    〜富()
    {
        处置(假);
    }

}
 

解决方案
  

当我刚刚重写的Dispose(BOOL处置)调用,那感觉真的很奇怪,说明我实现IDisposable,而无需显式的Dispose()函数(只是利用继承的一个),但有一切。

这是你不应该与关注。

当你继承了IDisposable的类,所有的Dispose模式水管已被基类为您处理。你真的应该做什么,但覆盖受保护的Dispose(布尔)方法,并跟踪您是否已经已经丢弃(正常筹集的ObjectDisposedException

有关详细信息,请参阅<一个我的博客文章href="http://reedcopsey.com/2009/03/30/idisposable-part-2-subclass-from-an-idisposable-class/">Subclassing从IDisposable的类。


此外,通常情况下,这是考虑封装继承它的IDisposable的类,而不是一个好主意。有些时候,继承了IDisposable的类是适当的,但他们是有些罕见。封装往往是一个更好的选择。

I have a parent and child class that both need to implement IDisposable. Where should virtual (and base.Dispose()?) calls come into play? When I just override the Dispose(bool disposing) call, it feels really strange stating that I implement IDisposable without having an explicit Dispose() function (just utilizing the inherited one), but having everything else.

What I had been doing (trivialized quite a bit):

internal class FooBase : IDisposable
{
    Socket baseSocket;

    private void SendNormalShutdown() { }

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

    private bool _disposed = false;
    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (disposing)
            {
                SendNormalShutdown();
            }
            baseSocket.Close();
        }
    }

    ~FooBase()
    {
        Dispose(false);
    }
}

internal class Foo : FooBase, IDisposable
{
    Socket extraSocket;

    private bool _disposed = false;
    protected override void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            extraSocket.Close();
        }
        base.Dispose(disposing);
    }

    ~Foo()
    {
        Dispose(false);
    }

}

解决方案

When I just override the Dispose(bool disposing) call, it feels really strange stating that I implement IDisposable without having an explicit Dispose() function (just utilizing the inherited one), but having everything else.

This is something you shouldn't be concerned with.

When you subclass an IDisposable class, all of the "Dispose pattern" plumbing is already being handled for you by the base class. You really should do nothing but override the protected Dispose(bool) method, and track whether you've been disposed already (to properly raise ObjectDisposedException.)

For details, see my blog post on Subclassing from an IDisposable class.


Also, often, it's a good idea to consider encapsulating the IDisposable class instead of subclassing it. There are times when subclassing an IDisposable class is appropriate, but they are somewhat rare. Encapsulation is often a better alternative.

这篇关于在一个子类实现IDisposable的时候,家长也实现IDisposable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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