1234567891011121314151617181920212223242526272829303132333435363738394041 |
- namespace PlcSiemens.Core.Common
- {
- /// <summary>
- /// 单例基类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class SingleBase<T> where T : new()
- {
- // ReSharper disable once InconsistentNaming
- private static readonly T _instance;
- // ReSharper disable once StaticFieldInGenericType
- private static readonly object ObjLock = new object();
- /// <summary>
- /// 获取单例句柄
- /// </summary>
- 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("单例模式设计,不允许重复实例化对象");
- }
- }
- }
- }
|