实现IDisposable C# [英] Implementing IDisposable C#

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

问题描述

我在理解IDisposable接口方面遇到了一些麻烦

I'm having a little trouble understanding the IDisposable interface

我正在开发一个游戏引擎,并对我的解决方案进行了代码分析,并被告知要在我的"GrapicsEngine"类上实现IDisposable接口,因为它包含一个Bitmap实例.

I'm developing a Game Engine and have ran code analysis on my solution and been told to implement the IDisposable interface on my "GrapicsEngine" class, because it contains a Bitmap instance.

自然地,我搜索了互联网以了解如何正确执行此操作,并提出了以下代码:

Naturally, I searched the internet to learn how to do this correctly and have came up with the following code:

这是我班的成员:

private const int BITMAP_WIDTH = 4096;
private const int BITMAP_HEIGHT = 4096;

private Graphics backBuffer;
private Bitmap bitmap;

private Color clearColor;

private WindowSurface surface;

private GraphicsQuality quality;

private Viewport viewport;

这是我的IDispoable区域:

and here is my IDispoable region:

#region IDisposable

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

protected virtual void Dispose(bool disposing)
{
    if (disposing == true)
        ReleaseManagedResources();

    ReleaseUnmanagedResources();
}

private void ReleaseManagedResources()
{
    if (bitmap != null)
        bitmap.Dispose();
    if (backBuffer != null)
        backBuffer.Dispose();
}

private void ReleaseUnmanagedResources()
{

}

~GraphicsEngine()
{
    Dispose(false);
}

#endregion

请注意,WindowSurface是WinForm面板,GraphicsQuality是枚举,并且视口仅包含int值.

Note that WindowSurface is a WinForm Panel, GraphicsQuality is an enum and Viewport contains only int values.

所以我只有几个问题:

  1. 我可以正确地处理我的资源吗?
  2. 我应该在非托管资源"方法中处理哪些资源
  3. 如果我创建一个新类并且它包含我的GraphicsEngine类,那么该类也应该实现IDisposable吗?

任何有关此主题的帮助将不胜感激.

Any help on this topic would be greatly appreciated.

推荐答案

您处在正确的轨道上.任何时候只要您拥有可处理的类级别变量,包含的类也应该是可处理的.据我所知,您正在正确地处理它.我没有看到类名称行,所以我无法确定您是否包含IDisposable接口,但是我想您已经实现了这些方法,所以我想您也可以.如果没有,请确保将其添加.
IDisposable是实现的连锁反应类型.如果变量是可抛弃的,并且仅在方法调用本地,则在调用结束时将其处置,但如果在类级别,则实现IDisposable并将其随同您的类一起处置.这样,使用您的课程的任何人都可以正确处理它.

You're on the right track. Anytime you have a class level variable that is disposable the containing class should also be disposable. And you're handling it properly from what I can tell. I don't see class name line so I can't tell if you have the IDisposable interface included but I imagine you do since you have implemented the methods. If not make sure you add it.
IDisposable is a chain reaction type of implementation. If the variable is disposable, and it's only local to a method call then you dispose of it at the end of the call, but if it's at the class level you implement IDisposable and dispose of it with your class as you're doing. That way anyone using your class can dispose of it properly.

例如:说我在我的课上打开了一个文件...

So for example: Say I have a file open in my class...

public class MyClass
{
     private File txtFile = File.Create(...)
}

现在有人在使用我的课程.

Now someone uses my class.

private void useClass()
{
     var myClass = new MyClass();
}

好吧,他们刚刚打开了一个文件,但没有正确处理.

Well, they have just opened a file and it wasn't disposed of properly.

修改代码,可以像这样使用它...

Modify the code and it can be used like so...

public sealed class MyClass : IDisposable
{
     private File txtFile = new File.Create(...)

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

     ~MyClass() => Dispose();
}

使用时,他们可以像这样使用它...

And when the use it they can use it like so...

private void useClass()
{
     using (var myClass = new MyClass())
     {
         //some code
     }
}

希望这能回答您的问题.只需记住,是否在方法的局部声明了一个可抛弃的对象,然后就不必在类中实现IDisposable了,因为您将在该方法中处理它.但是,如果您在类级别的范围内实现它,则无论是否有处理它的方法,都应实现IDisposable并检查以确保在包含类调用的那一部分被处理时它已被处理.有道理? 希望这会有所帮助.

Hopefully this answers your questions. Just remember, is you declare a disposable object local to a method then you don't have to implement IDisposable in your class because you're going to dispose of it in that method. But if you implement it at class level scope, whether you have a method disposing it or not, you should implement IDisposable and check to make sure it's disposed when that containing class calls dispose. Make sense? Hope this helps.

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

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