SingletonProvider.cs 937 B

12345678910111213141516171819202122232425262728293031323334353637
  1. namespace WWPipeLine.Commons
  2. {
  3. /// <summary>
  4. /// 泛型单例模式
  5. /// </summary>
  6. /// <typeparam name="T"></typeparam>
  7. public class SingletonProvider<T> where T : class, new()
  8. {
  9. private static T _instance = null;
  10. private static object _instanceLock = new object();
  11. private SingletonProvider()
  12. {
  13. }
  14. /// <summary>
  15. /// 用法:SingletonProvider&lt;T&gt;.Instance 获取该类的单例
  16. /// </summary>
  17. public static T Instance
  18. {
  19. get
  20. {
  21. if (_instance == null)
  22. {
  23. lock (_instanceLock)
  24. {
  25. if (_instance == null)
  26. {
  27. _instance = new T();
  28. }
  29. }
  30. }
  31. return _instance;
  32. }
  33. }
  34. }
  35. }