123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System;
- namespace Houdar.Core.Util.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("单例模式设计,不允许重复实例化对象");
- }
- }
- }
- }
|