简体   繁体   English

在 C# 中处理对象

[英]Disposing object in C#

I have written the following class:我编写了以下课程:

public class CoupleFrames
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;
}

Now I'm using the following code for disposing the variables.现在我使用以下代码来处理变量。

CoupleFrames cf = new CoupleFrames(frame1, frame2);
// some code...
cf.colorFrame.Dispose();
cf.desktopFrame.Dispose();

I'm not sure that this is the correct way.我不确定这是正确的方法。 Someone can suggest me the correct way for disposing the entire object?有人可以建议我处理整个对象的正确方法吗?

I'm not sure that this is the correct way. 我不确定这是正确的方法。 Someone can suggest me the correct way for disposing the entire object? 有人可以建议我处理整个物体的正确方法吗?

Sure - you should make CoupleFrames implement IDisposable , and its Dispose method should dispose of the objects it "owns". 当然 - 你应该让CoupleFrames实现IDisposable它的 Dispose方法应该处理它“拥有”的对象。 For example: 例如:

public sealed class CoupleFrames : IDisposable
{
    private readonly ColorImageFrame colorFrame;
    private readonly Bitmap desktopFrame;

    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        // TODO: Argument validation, unless it's valid for these parameters
        // to be null, in which case the Dispose method would need to be careful.
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public void Dispose()
    {
        colorFrame.Dispose();
        desktopFrame.Dispose();
    }
}

A few points to note: 需要注意的几点:

  • You should make sure it's clear that the CoupleFrame really "owns" these constituent objects. 你应该确保CoupleFrame真正“拥有”这些组成对象。 Disposal relies on a clear ownership model 处置依赖于明确的所有权模式
  • If CoupleFrame isn't sealed (and can't be) you may need to go into a more complicated pattern with virtual methods and finalizers. 如果CoupleFrame 没有密封(并且不能),您可能需要使用虚拟方法和终结器进入更复杂的模式。 It can get very complicated, and you should read the advice given here by Joe Duffy et al . 它会变得非常复杂,你应该阅读Joe Duffy等人给出建议 If your class is sealed, a lot of that complexity goes away 如果你的课程是密封的,那么很多复杂性就会消失
  • Public fields are generally a bad idea (in terms of encapsulation), which is why I've made them private here. 公共字段通常是一个坏主意(在封装方面),这就是我在这里将它们设为私有的原因。 I've also made them readonly, as if they can be changed later you need to think about whether changing them should dispose of the previously-referenced object etc. 我也把它们做成只读,好像它们可以在以后更改时你需要考虑是否应该更改它们应该处理先前引用的对象等。
  • By making CoupleFrame implement IDisposable , you're basically telling all clients that they should dispose of any instance they own. 通过使CoupleFrame实现IDisposable ,您基本上告诉所有客户他们应该处置他们拥有的任何实例。 If you're not happy with imposing that burden, you need to rethink the design a bit. 如果你不满意这种负担,你需要重新考虑一下设计。

I would implement the Dispose pattern 我会实现Dispose模式

public class CoupleFrames : IDisposable
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;

    private bool disposed;

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

    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
        {
            return;
        }
        if (disposing)
        {
            colorFrame.Dispose();
            desktopFrame.Dispose();
        }
        disposed = true;
    }
}

Implement IDisposable in your class and have it dispose of the member variables from there. 在你的类中实现IDisposable并让它从那里处理成员变量。

public class CoupleFrames : IDisposable
        {
            public CoupleFrames(ColorImageFrame cif, Bitmap df)
            {
                this.colorFrame = cif;
                this.desktopFrame = df;
            }

            public ColorImageFrame colorFrame;
            public Bitmap desktopFrame;

            public void Dispose()
            {
                this.colorFrame.Dispose();
                this.desktopFrame.Dispose();
            }
        }

You can use the IDisposable interface. 您可以使用IDisposable接口。

public class CoupleFrames : IDisposable
{
    ....

    public void Dispose()
    {
        // Your disposing code here
    }

    ~CoupleFrames()
    {
        Dispose();
    }
}

You can use the destructor to call the Dispose method since the object can sometimes be deleted by the GC. 您可以使用析构函数来调用Dispose方法,因为GC有时可以删除该对象。

Make CoupleFrames Implement the Idisposable Interface. 使CoupleFrames实现Idisposable接口。

public class CoupleFrames : IDisposable
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;

    public void Dispose()
    {
        cf.colorFrame.Dispose();
        cf.desktopFrame.Dispose();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM