c# 静态成员如何以及何时被处置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12126598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How and when are c# Static members disposed?
提问by Joe
I have a class with extensive static members, some of which keep references to managed and unmanaged objects.
我有一个包含大量静态成员的类,其中一些保持对托管和非托管对象的引用。
For instance, the static constructor is called as soon as the Type is referenced, which causes my class to spin up a blockingQueue of Tasks. This happens when one of the static methods is called, for example.
例如,一旦引用了类型,就会调用静态构造函数,这会导致我的类启动一个任务阻塞队列。例如,当调用静态方法之一时会发生这种情况。
I implemented IDisposable, which gives me methods to handle disposal on any instance objects I created. However, these methods are never called if the consumer doesn't create any instance objects from my class.
我实现了 IDisposable,它为我提供了处理我创建的任何实例对象的处理方法。但是,如果使用者没有从我的类中创建任何实例对象,则永远不会调用这些方法。
How and where do I put code to dispose of references maintained by the static portion of my class? I always thought that disposal of static-referenced resources happened when the last instance object was released; this is the first time I've ever created a class where no instances may ever be created.
我如何以及在哪里放置代码来处理由我的类的静态部分维护的引用?我一直认为静态引用资源的处置发生在最后一个实例对象被释放时;这是我第一次创建一个不可以创建任何实例的类。
采纳答案by dasblinkenlight
The static variable of your class are not garbage collected until the app domain hosting your class is unloaded. The Dispose()method will not be called, because it is an instance method, and you said that you wouldn't create any instances of your class.
在卸载托管您的类的应用程序域之前,您的类的静态变量不会被垃圾收集。Dispose()不会调用该方法,因为它是一个实例方法,并且您说过不会创建类的任何实例。
If you would like to make use of the Dispose()method, make your object a singleton, create one instance of it, and dispose of it explicitly when your application is about to exit.
如果您想使用该Dispose()方法,请将您的对象设为单例,创建它的一个实例,并在您的应用程序即将退出时显式处理它。
public class MyClass : IDisposable {
public IList List1<int> {get; private set;}
public IDictionary<string,string> Dict1 {get; private set;}
public void Dispose() {
// Do something here
}
public static MyClass Instance {get; private set;}
static MyClass() {
Instance = new MyClass();
}
public static void DisposeInstance() {
if (instance != null) {
Instance.Dispose();
Instance = null;
}
}
}
回答by oxilumin
You should dispose this objects manually, there is no way to create a "finalizer" for static resources.
您应该手动处理这些对象,无法为静态资源创建“终结器”。
回答by Guido Kleijer
public class Logger : IDisposable
{
private string _logDirectory = null;
private static Logger _instance = null;
private Logger() : this(ConfigurationManager.AppSettings["LogDirectory"])
{
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}
private Logger(string logDirectory)
{
}
public static Logger Instance
{
get
{
if (_instance == null)
_instance = new Logger();
return _instance;
}
}
private void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
Dispose();
}
public void Dispose()
{
// Dispose unmanaged resources
}
}
回答by ilias iliadis
If you really want to have static members which keep references to unmanaged objects just create a method for disposing the unmanaged objects and "force" consumer to use it on exit.
如果您真的想要拥有保留对非托管对象的引用的静态成员,只需创建一个方法来处理非托管对象并“强制”消费者在退出时使用它。
By "force" I mean document your class with a paragraph that states "when" and "why" to use this "dispose" method. Do it either if you are the sole consumer (or your code...) or you plan to distribute your class. Also try to use a somehow descriptive name (to that "dispose" method) such as "DisposeStatics", "AlwaysDispose", "DisposeAtEnd" etc.
通过“强制”,我的意思是用一段说明“何时”和“为什么”使用这种“处置”方法的段落来记录你的班级。如果您是唯一的消费者(或您的代码...),或者您打算分发您的课程,请执行此操作。还尝试使用某种描述性名称(针对该“处置”方法),例如“DisposeStatics”、“AlwaysDispose”、“DisposeAtEnd”等。

