C#如何实现Dispose方法 [英] C# how to implement Dispose method

查看:671
本文介绍了C#如何实现Dispose方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关实现 Dispose 方法的建议。

I need some advice on the implementation of the Dispose method.

在我们的应用程序中,用户设计了自己的自己的用户界面。我有一个预览窗口,显示UI的外观。此UI中绘制的所有对象最终都派生自公共基类ScreenObject。我的预览管理器包含对ScreenGrid的单个对象引用,该对象是整个预览区域的网格对象。

In our application the user designs their own UI. I have a preview window that shows what the UI is going to look like. All object drawn in this UI ultimately derive from a common base class ScreenObject. My preview manager contain a single object reference to a ScreenGrid which is the grid object for the entire preview area.

问题#1

我的某些派生屏幕类保留着非托管资源,例如数据库连接,位图图像和 WebBrowser 控件。这些类需要处理这些对象。我在基类 ScreenObject 的基类中创建了一个虚拟的 Dispose 方法,然后实现了一个重写 Dispose包含非托管资源的每个派生类中的方法。但是,现在我刚刚创建了一个名为 Dispose 的方法,我没有实现 IDisposable 。我应该实现 IDisposable 吗?如果是这样,我该如何实现呢?

Some of my derived screen classes hold onto unmanaged resources, such as a database connection, bitmap image and a WebBrowser control. These classes need to dispose of these objects. I created a virtual Dispose method in the base ScreenObject base class and then implemented an override Dispose method in each of the derived classes that hold onto unmanaged resources. However, right now I just created a method called Dispose, I am not implementing IDisposable. Should I implement IDisposable? If so how do I implement it?


  • 仅对具有非托管资源的派生类

  • 具有非托管资源的基类和派生类 OR

  • 基类和所有派生类,包括那些没有非托管资源的类

在没有非托管资源的基类中放置虚拟 Dispose 方法是否错误?以便您可以利用多态性?

Is it wrong to put a virtual Dispose method in a base class that doesn't have unmanaged resources so that you can take advantage of polymorphism?

问题#2

阅读有关 Dispose 方法和 IDisposable 接口的信息微软声明,处置对象应仅调用为其父项处理方法。父级将为其父级调用它,依此类推。对我来说,这似乎是倒退。我可能想处置一个孩子,但要保留其父母。

In reading about the Dispose method and the IDisposable interface Microsoft states that the disposing object should only call the Dispose method for its parent. The parent will call it for its parent and so on. To me this seems backwards. I may want to dispose of a child but keep its parent around.

我认为应该相反,处置的对象应处置其孩子。然后孩子们应该处置他们的孩子,依此类推。

I would think it should be the other way around, an object being disposed should dispose of its children. The children should then dispose of their children and so on.

我在这里错了还是我错过了什么吗?

Am I wrong here or am I missing something?

推荐答案

问题1:也使用以下模式实现 IDisposable

Question 1: Implement IDisposable as well, using the following pattern:

public class MyClass : IDisposable
{
    bool disposed;

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                //dispose managed resources
            }
        }
        //dispose unmanaged resources
        disposed = true;
    }

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

问题2:Microsoft的意思是派生类调用置于其父类上。实例的所有者仅对派生程度最高的类型调用Dispose。

Question 2: What Microsoft means is that a derived class calls dispose on it's parent class. The owner of the instance only calls Dispose on the most derived type.

一个(简短的)示例:

class Parent : IDisposable 
{
    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                //dispose managed resources
            }
        }
        //dispose unmanaged resources
        disposed = true;
    }

}
class Child : Parent, IDisposable 
{ 
    protected override void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                //dispose managed resources
            }
            base.Dispose(disposing);
        }
        //dispose unmanaged resources
        disposed = true;
    }

}
class Owner:IDisposable
{
    Child child = new Child();
    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                if(child!=null)
                {
                    child.Dispose();
                }
            }
        }
        //dispose unmanaged ressources
        disposed = true;
    }
}

所有者仅呼叫处置放在孩子身上,而不在父母身上。孩子负责在父级上调用处置

The owner only calls Dispose on the Child, but not on the Parent. The Child is responsible for calling Dispose on the Parent.

这篇关于C#如何实现Dispose方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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