C# 单例模式使用 Singleton
Singleton 类如下:
public class Singleton<T> where T : class, new()
{
private static T _instance;
private static readonly object syslock = new object();
public static T getInstance()
{
if (_instance == null)
{
lock (syslock)
{
if (_instance == null)
{
_instance = new T();
}
}
}
return _instance;
}
}
如要实现单例的继承 Singleton
public class T1 : Singleton<T1>
{
public void Test()
{
}
}
调用
T1.getInstance().Test();