Singleton.cs 553 B

12345678910111213141516171819202122232425
  1. using System;
  2. public class Singleton<T> where T : class
  3. {
  4. private static T _instance;
  5. private static readonly object _lock = new object();
  6. public static T Instance
  7. {
  8. get
  9. {
  10. if (_instance == null)
  11. {
  12. lock (_lock)
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = (T)Activator.CreateInstance(typeof(T), true);
  17. }
  18. }
  19. }
  20. return _instance;
  21. }
  22. }
  23. }