Singleton.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CSharpUtil
  7. {
  8. /// <summary>
  9. /// 普通单例
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. public class Singleton<T> where T : Singleton<T>, new()
  13. {
  14. /// <summary>
  15. /// 单例
  16. /// </summary>
  17. private static T m_Instance;
  18. /// <summary>
  19. /// 实例
  20. /// </summary>
  21. public static T Instance
  22. {
  23. get
  24. {
  25. if (object.ReferenceEquals(Singleton<T>.m_Instance, null))
  26. {
  27. var obj = new T();
  28. Singleton<T>.m_Instance = obj;
  29. Container.Add(typeof(T).Name, obj);
  30. }
  31. return Singleton<T>.m_Instance;
  32. }
  33. }
  34. #region ' 内部容器 '
  35. static Dictionary<string, Object> _mC;
  36. static Dictionary<string, Object> Container
  37. {
  38. get
  39. {
  40. if (null == _mC)
  41. {
  42. _mC = new Dictionary<string, object>();
  43. }
  44. return _mC;
  45. }
  46. }
  47. #endregion
  48. }
  49. }