[英]What can I do for a static class to be reloaded/reinstantiated?
我使用的一個應用程序有一個 static class 從其構造函數中的 XML 文件加載一些配置。 但是當我們對這些 XML 之一進行更改時,這個 class 不會重新加載(應該是這樣,因為它是靜態的)。 我該怎么做才能讓這個 static class 再次實例化,重新加載配置? 我需要重新啟動 IIS 服務器嗎? 還有其他一些方法嗎?
使用帶有鎖和數據失效的 Singleton 模式可能更好
(在這里輸入很抱歉,任何語法都是錯誤的)
public class MySingleton
{
private static MySingleton _instance;
private static object _lock = new object();
private static MySingleton
{
// initialize here
}
public static MySingleton Instance
{
get
{
var singl = _instance;
if (singl != null)
return singl;
lock(_lock)
{
if (singl != null)
return singl;
_instance = new MySingleton();
return _instance;
}
}
}
public static void Invalidate()
{
lock(_lock)
{
_instance = null;
}
}
// -- your non-static methods
public bool CheckSomething(){ return true; }
}
利用
// thread one
if (MySingleton.Instance.CheckSomething())
// my code
// thread two
MySingleton.Invalidate();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.