Singleton.cs 663 B

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