C# 正确处理成员变量中的可支配资源?

C# 正确处理成员变量中的可支配资源?,c#,opengl,idisposable,using,C#,Opengl,Idisposable,Using,我有一个“游戏课”,看起来像这样: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenTK; namespace MyGame { class Game : GameWindow { Player player; List<Enemy>

我有一个“游戏课”,看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;

namespace MyGame
{
    class Game : GameWindow
    {
        Player player;
        List<Enemy> enemies;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Initialize stuff here
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            // Call Update on the player and the enemies
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            // Call Draw on the player and the enemies
        }

        static void Main(string[] args)
        {
            using (var game = new Game())
            {
                game.Run();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用OpenTK;
名称空间MyGame
{
类游戏:游戏窗口
{
玩家;
列出敌人;
受保护的覆盖无效加载(事件参数e)
{
基础荷载(e);
//在这里初始化东西
}
受保护的覆盖无效OnUpdate帧(FrameEventArgs e)
{
基础.更新帧(e);
//呼叫玩家和敌人的更新
}
RenderFrame上受保护的覆盖无效(FrameEventArgs e)
{
基于渲染帧(e);
//对玩家和敌人召唤抽签
}
静态void Main(字符串[]参数)
{
使用(var game=new game())
{
game.Run();
}
}
}
}

但现在我遇到了障碍。
播放器
对象和
敌人
对象现在需要保存对OpenGL缓冲区的引用,这些缓冲区需要手动分配和释放。在C#中,针对此类问题提出的解决方案似乎是使用
IDisposable
<代码>使用似乎可以方便地将其保存在一个方法中。但是如果将其作为一个成员变量,如何以最佳方式解决此问题呢?

我希望您正在寻找:

class Game : GameWindow, IDisposable
{
        private bool _disposed;

  public void Dispose()
        {
            // If this function is being called the user wants to release the
            // resources. lets call the Dispose which will do this for us.
            Dispose(true);

            // Now since we have done the cleanup already there is nothing left
            // for the Finalizer to do. So lets tell the GC not to call it later.
            GC.SuppressFinalize(this);
        }

 /// <summary>
        ///     Dispose client here
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (your_any_managed_unmanaged_resource != null)
                    {
                       //do cleanup
                    }
                }

                _disposed = true;
            }
        }
}

是否可以将缓冲区包装到托管对象中?通常,IDisposable用于包装非托管资源,并以这样的方式实现,即您的其他类不必将IDisposable接口一直传播到您的继承层次结构上。我不确定您的确切意思。什么是托管对象?你能给我一个代码示例吗?基本上,托管方式是由.NET创建的,因此在CLR的控制下。因此OGL缓冲区是非托管的。解决方案与其说是代码,不如说是设计。如果可能,您需要一个类,比如说
OGLBufferWrapper
,它将实现
IDisposable
。这将处理与缓冲区有关的所有事情。然后在你的
玩家
敌人
中,任何需要缓冲区的东西都可以使用(var buffer=new OGLBufferWrapper(){…}。如果您无法找到一种方法来实现这一点,您可能必须在对象层次结构的整个过程中实现
IDisposable
。但是填充缓冲区的速度非常慢,因此我希望在调用之间保持缓冲区。在这种情况下,您可能只需要在其他对象上实现IDisposable(如@Amit的回答所示)哦,我刚刚意识到GameWindow已经是一个IDisposable了。
Game g = new Game();
g.Play();

g.Dispose();