singleton.cs 634 B

123456789101112131415161718192021222324
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace tool
  5. {
  6. // 由于单例基类不能实例化,故设计为抽象类
  7. public abstract class Singleton<T> where T : class
  8. {
  9. class Nested
  10. {
  11. // 创建模板类实例,参数2设为true表示支持私有构造函数
  12. internal static readonly T instance = Activator.CreateInstance(typeof(T), true) as T;
  13. }
  14. private static T instance = null;
  15. public static T Instance
  16. {
  17. get
  18. {
  19. return Nested.instance;
  20. }
  21. }
  22. }
  23. }