从组件派生并正确实现IDisposable

时间:2010-05-20 15:26:23

标签: c# c#-2.0 components idisposable

我有一个Visual Studio 2008 C#.NET 2.0 CF项目,其中包含一个派生自Component的抽象类。从该课程中,我得出了几个具体的类(如下面的例子)。 但是,当我去退出我的Form时,虽然调用了Form的Dispose()成员并调用了components.Dispose(),但我的组件从未被释放。

有人可以建议我如何修复这个设计吗?

public abstract class SomeDisposableComponentBase : Component
{
    private System.ComponentModel.IContainer components;

    protected SomeDisposableComponentBase()
    {
        Initializecomponent();
    }

    protected SomeDisposableComponentBase(IContainer container)
    {
        container.Add(this);
        Initializecomponent();
    }

    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
    }

    protected abstract void Foo();

    #region IDisposable Members
    bool disposed_;

    /// Warning 60 CA1063 : Microsoft.Design : Ensure that 'SomeDisposableComponentBase.Dispose()' is declared as public and sealed.*
    public void Dispose()
    {
        // never called
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        // never called
         if (!disposed_)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            disposed_ = true;
        }
        base.Dispose(disposing);
    }
    #endregion    
}

public SomeDisposableComponent : SomeDisposableComponentBase
{
    public SomeDisposableComponent() : base()
    {
    }

    public SomeDisposableComponent(IContainer container) : base(container)
    {
    }

    protected override void Foo()
    {
        // Do something...
    }

    protected override void Dispose(bool disposing)
    {
        // never called
        base.Dispose(disposing);
    }
}

public partial class my_form : Form
{
    private SomeDisposableComponentBase d_;

    public my_form()
    {
        InitializeComponent();
        if (null == components)
            components = new System.ComponentModel.Container();

        d_ = new SomeDisposableComponent(components);
    }

    /// exit button clicked
    private void Exit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    /// from the my_form.designer.cs
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            // this function is executed as expected when the form is closed
            components.Dispose();
        }
        base.Dispose(disposing);
    }
}

*我注意到FX-Cop在这里给我一个提示。但是,如果我尝试将该函数声明为密封,我会收到错误:

error CS0238: 'SomeDisposableComponentBase.Dispose()' cannot be sealed because it is not an override

声明该功能的覆盖导致:

'SomeDisposableComponentBase.Dispose()': cannot override inherited member 'System.ComponentModel.Component.Dispose()' because it is not marked virtual, abstract, or override

谢谢, PaulH

2 个答案:

答案 0 :(得分:7)

SomeDisposableComponentBase 覆盖 Component.Dispose(Boolean)

您还需要删除SomeDisposableComponentBase.Dispose()方法(不带参数)because it hides the Component.Dispose implementation,因此根据您声明变量的方式将它们视为不同的方法:

using (Component component = new SomeDisposableComponent()) {
    // Calls Component.Dispose upon exiting the using block
}

using (SomeDisposableComponentBase component = new SomeDisposableComponent()) {
    // Calls SomeDisposableComponentBase.Dispose upon existing the using block
}

答案 1 :(得分:5)

正在发生的事情是编译器将Dispose on SomeDisposableComponentBase上的方法解释为

new public void Dispose() 
{ 
    // never called 
    Dispose(true); 
    GC.SuppressFinalize(this); 
} 

不是覆盖,它为您的类层次结构中的现有名称提供了新的语义。因此,实际上您正在创建一个与Component.Dispose不同的新Dispose方法。

查看Implementing Finalize and Dispose to Clean Up Unmanaged Resources文章,了解有关如何实施IDisposable.Dispose方法的官方指南。