35 lines
894 B
C#
35 lines
894 B
C#
namespace BigSpace.XRCore.Base
|
|
{
|
|
public abstract class Singleton<T> where T : class, new()
|
|
{
|
|
private static readonly object _lock = new object();
|
|
private static T _instance;
|
|
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new T();
|
|
}
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
protected Singleton()
|
|
{
|
|
// 防止通过反射创建多个实例
|
|
if (_instance != null)
|
|
{
|
|
throw new System.Exception($"An instance of {typeof(T)} already exists!");
|
|
}
|
|
}
|
|
}
|
|
} |