namespace PlcSiemens.Core.Common
{
///
/// 单例基类
///
///
public class SingleBase where T : new()
{
// ReSharper disable once InconsistentNaming
private static readonly T _instance;
// ReSharper disable once StaticFieldInGenericType
private static readonly object ObjLock = new object();
///
/// 获取单例句柄
///
public static T Instance
{ get { return _instance; } }
static SingleBase()
{
if (_instance != null) return;
lock (ObjLock)
{
if (_instance == null)
{
_instance = new T();
}
}
}
public SingleBase()
{
if (_instance != null)
{
throw new InvalidOperationException("单例模式设计,不允许重复实例化对象");
}
}
}
}