加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从组件派生并正确实现IDisposable

发布时间:2020-12-15 23:55:18 所属栏目:百科 来源:网络整理
导读:我有一个Visual Studio 2008 C#.NET 2.0 CF项目,其中一个抽象类派生自Component.从该课程中,我得出了几个具体的类(如下面的例子). 但是,当我去退出我的Form时,虽然调用了Form的Dispose()成员并且调用了components.Dispose(),但我的组件从未被释放. 任何人都
我有一个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

解决方法

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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读