Singleton.cs 682 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Aitex.Core.Util
  6. {
  7. [Serializable]
  8. public class Singleton<T> where T : class, new()
  9. {
  10. private static volatile T instance;
  11. private static object locker = new Object();
  12. public Singleton() { }
  13. public static T Instance
  14. {
  15. get
  16. {
  17. if (instance == null)
  18. {
  19. lock (locker)
  20. {
  21. if (instance == null) instance = new T();
  22. }
  23. }
  24. return instance;
  25. }
  26. }
  27. }
  28. }