简体   繁体   English

为什么静态类不能具有析构函数?

[英]Why can't static classes have destructors?

Two parts to this: 分为两部分:

  1. If a static class can have a static constructor, why can't it have a static destructor? 如果静态类可以具有静态构造函数,为什么它不能具有静态析构函数?

  2. What is the best workaround? 最好的解决方法是什么? I have a static class that manages a pool of connections that are COM objects, and I need to make sure their connections get closed/released if something blows up elsewhere in the program. 我有一个静态类来管理作为COM对象的连接池,并且如果程序中其他地方发生问题,我需要确保其连接被关闭/释放。

Instead of a static class, you should use a normal class with the singleton pattern (that is, you keep one single instance of the class, perhaps referenced by one static property on the class itself). 您应该使用具有单例模式的普通类(而不是静态类)(即,保留该类的单个实例,也许由类本身的一个静态属性引用)。 Then you can have a destructor, or even better, a combination of destructor and Dispose method. 然后,您可以拥有一个析构函数,或者甚至可以拥有一个析构函数和Dispose方法的组合。

For example, if you now have: 例如,如果您现在拥有:

static class MyClass
{
    public static void MyMethod() {...}
}

//Using the class:
MyClass.MyMethod();

you would have instead: 您将改为:

class MyClass : IDisposable
{
    public static MyClass()
    {
        Instance=new MyClass();
    }

    public static MyClass Instance {get; private set;}

    public void MyMethod() {...}

    public void Dispose()
    {
        //...
    }

    ~MyClass()
    {
        //Your destructor goes here
    }
}

//Using the class:
MyClass.Instance.MyMethod();

(Notice how the instance is created in the static constructor, which is invoked the first time that any of the class static members is referenced) (注意如何在静态构造函数中创建实例,该静态构造函数是在首次引用任何类静态成员时调用的)

  1. Static classes don't have destructors because a static class never gets destroyed. 静态类没有析构函数,因为静态类永远不会被破坏。

  2. If you want to create and destroy multiple instances of it, it shouldn't be static. 如果要创建和销毁它的多个实例,则它不应是静态的。 Make it a full class. 使它成为一堂完整的课。

  3. Destructors shouldn't be used for this purpose anyway. 无论如何,析构函数都不应该用于此目的。 Use IDisposable / Dispose. 使用IDisposable /处置。

1. Why? 1.为什么? -- A type cannot have a constructor per se as in how you usually think of constructors on instances. -类型本身不能具有构造函数,就像您通常在实例上如何看待构造函数一样。 In general it's sometimes known as a "static initializer" method but Microsoft uses the terminology "type constructor" (and it has special restrictions) - you put code in it to init the type/class - if it was an instance constructor it could be overloaded. 通常,它有时被称为“静态初始值设定项”方法,但是Microsoft使用术语“类型构造函数”(并且有特殊限制)-您将代码放入其中以初始化类型/类-如果它是实例构造函数,则可能是超载。 This static restriction on "type constructor" is because .NET CLR is responsible for loading the class template on the heap and doesn't allow parameters to be specified under this circumstance (because how would you ever pass arguments). 对“类型构造函数”的静态限制是因为.NET CLR负责将类模板加载到堆上,并且不允许在这种情况下指定参数(因为您将如何传递参数)。 Because in the strictest sense the programmer is not responsible for causing the type constructor to be invoked, it wouldn't make much sense to allow the programmer to code a static destructor when it's more in the CLR's domain. 因为从最严格的意义上讲,程序员不负责导致类型构造函数被调用,所以当它在CLR领域中时,允许程序员编写静态析构函数就没有多大意义。 The CLR will eventually remove the class template from the heap, but the class template's lifetime is longer than its instances, so you wouldn't want to do anything resource intensive in it anyway (eg hold open a db connection). CLR最终会从堆中删除类模板,但是类模板的生存期比其实例更长,因此无论如何您都不希望在其中进行任何资源密集型工作(例如,保持打开数据库连接)。

2. What? 2.什么? - Singleton If you are running into a circumstance where you feel you need to open a resource on the class template and destroy it afterward, you might consider the Singleton software pattern to have only one instance of that class and possibly also implement the System.IDiposable interface to aid with cleanup, in addition to the destructor. -Singleton如果您遇到需要在类模板上打开资源并随后销毁它的情况,则可以考虑Singleton软件模式仅具有该类的一个实例,并且可能还实现System.IDiposable接口,除了析构函数外,还有助于清理。 (I see somebody has already beaten me to the IDisposable code sample first, so I will end my solution here.) (我看到有人首先将我击败了IDisposable代码示例,所以在这里结束我的解决方案。)

A static class is never destroyed. 静态类永远不会被破坏。 It's terminated together with the program. 它与程序一起终止。 You could use the singleton pattern as an implemenation instead of using a static class 您可以使用单例模式作为实现,而不是使用静态类

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

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