简体   繁体   English

C#make class static?

[英]C# make class static?

I have a class like that: 我有一个这样的课:

class ContentManager : IDisposable
{
    List<int> idlist = new List<int>();

    public int Load(string path)
    {
        //Load file, give content, gets an id

        //...

        int id = LoadFile(myfilecontent);

        idlist.Add(id);
        return id;
    }

    public void Dispose()
    {
        //Delete the given content by id, stored in idlist

        foreach(int id in idlist)
        {
            DeleteContent(id);
        }
    }
}

I want to make it static, because i need only one instance and can access the function from every other class without giving an instance. 我想让它成为静态的,因为我只需要一个实例,并且可以在不提供实例的情况下从其他所有类访问该函数。

I can make every variable in it static and the functions static. 我可以使其中的每个变量都是静态的,函数是静态的。

But my problem is this IDisposable. 但我的问题是这个IDisposable。 I cannot have Interfaces in static classes. 我不能在静态类中使用接口。 How can i do some action at the end? 我怎么能在最后做一些动作? I mean i can remove that interface but leave the function in it and use my main class and when my main class gets disposed i call ContentManager.Dispose(). 我的意思是我可以删除该接口,但将函数保留在其中并使用我的主类,当我的主类被释放时,我调用ContentManager.Dispose()。 But when i forget that in my main... 但是当我忘记在我的主...

Do you have a good solution for that? 你有一个很好的解决方案吗? Make sure that Dispose is called every time when the program gets closed? 确保每次程序关闭时都调用Dispose?

Edit: I load data in a graphic card and get the pointer back. 编辑:我在图形卡中加载数据并返回指针。 When my application closes, i need to delete the contents from the graphics card. 当我的应用程序关闭时,我需要从显卡中删除内容。 To be safe, everything is deleted, i use dispose. 为了安全起见,一切都被删除了,我使用了dispose。

I would leave your class as a non-static class and implement the singleton pattern. 我会把你的类留作非静态类并实现单例模式。 I added an example of how you would use it as singleton: 我添加了一个如何将其用作单例的示例:

public class ContentManager : IDisposable
{
    private List<int> idlist = new List<int>();
    private static ContentManager instance;

    private ContentManager () {}

    public static ContentManager Instance
    {
       get 
       {
           if (instance == null)
           {
               instance = new ContentManager ();
           }
           return instance;
       }
    }

    public int Load(string path)
    {
        int id = LoadFile(myfilecontent);    
        idlist.Add(id);
        return id;
    }

    public void Dispose()
    {
        foreach(int id in idlist)
        {
            DeleteContent(id);
        }
    }
}

You should just not implement IDisposable . 你应该不实现IDisposable The thing is, that interfaces purpose is to ensure that clean up is done when an instance goes out of scope. 问题是,接口的目的是确保在实例超出范围时进行清理。 There are no instances, and because of that IDisposable doesn't really apply. 没有实例,因此IDisposable并不真正适用。

The main thing that IDisposable gives you is that if you allocate your instance in a using statement the dispose call is added for you by the compiler. IDisposable为您提供的主要功能是,如果您在using语句中分配实例,则编译器会为您添加dispose调用。 Again, you no longer have instances so it has no purpose. 同样,您不再拥有实例,因此它没有任何意义。 Instead you should just have your own dispose/clean up method and document it's purpose. 相反,你应该有自己的处理/清理方法并记录它的目的。 If you think that is too error prone (relying on documentation is obviously less ideal than having the compiler/runtime enforce something) then you probably want to follow the singleton pattern as suggested in another answer. 如果您认为这太容易出错(依赖于文档显然不如编译器/运行时强制执行某些操作那么理想),那么您可能希望按照另一个答案中的建议遵循单例模式。

Why do you need an IDisposable interface in this case? 在这种情况下,为什么需要IDisposable接口? Static finalizers and destructors are not possible, because types are only unloaded when the AppDomain shuts down so anyway the fact that you could use IDisposable would not be useful. 静态终结器和析构函数是不可能的,因为只有当AppDomain关闭时才会卸载类型,所以无论如何,你可以使用IDisposable的事实都没有用。

If you really need to implement IDisposable and want to have just one instance of the class maybe it is a better solution to use a Singleton pattern? 如果你真的需要实现IDisposable并希望只有一个类的实例,那么使用Singleton模式是一个更好的解决方案吗?

Your class could then look like this: 您的课程可能如下所示:

 class ContentManager : IDisposable
 {
        List<int> idlist = new List<int>();

        static ContentManager instance=null;

        ContentManager()
        {
        }

        public static ContentManager Instance
        {
            get
            {
                if (instance==null)
                {
                    instance = new ContentManager();
                }
                return instance;
            }
        }

        public int Load(string path)
        {
            //Load file, give content, gets an id

            //...

            int id = LoadFile(myfilecontent);

            idlist.Add(id);
            return id;
        }

        public void Dispose()
        {
            //Delete the given content by id, stored in idlist

            foreach (int id in idlist)
            {
                DeleteContent(id);
            }
        }
 }

It is not thread-safe but could be enough in your scenario. 它不是线程安全的,但在您的场景中可能就足够了。

尝试将您的班级变成单身人士。

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

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