SingletonProperty.cs 894 B

12345678910111213141516171819202122232425262728293031323334
  1. namespace IFramework.Singleton
  2. {
  3. /// <summary>
  4. /// 单例属性
  5. /// </summary>
  6. /// <typeparam name="T"></typeparam>
  7. public static class SingletonProperty<T> where T : class, ISingleton
  8. {
  9. private static T _instance;
  10. private static readonly object lockObj = new object();
  11. /// <summary>
  12. /// 实例
  13. /// </summary>
  14. public static T instance
  15. {
  16. get
  17. {
  18. lock (lockObj)
  19. if (_instance == null)
  20. {
  21. _instance = SingletonCreator.CreateSingleton<T>();
  22. SingletonCollection.Set(_instance);
  23. }
  24. return _instance;
  25. }
  26. }
  27. /// <summary>
  28. /// 注销
  29. /// </summary>
  30. public static void Dispose() { _instance = null; }
  31. }
  32. }