IcsFromERPJob
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ICS.Common.DesignPattern
  8. {
  9. /// <summary>
  10. /// 单例模式
  11. /// </summary>
  12. public static class Singleton<T> where T : class, new()
  13. {
  14. // 定义一个静态变量来保存类的实例
  15. private static T uniqueInstance;
  16. // 定义一个标识确保线程同步
  17. private static object locker = null;
  18. // 定义私有构造函数,使外界不能创建该类实例
  19. static Singleton()
  20. {
  21. locker = new object();
  22. }
  23. /// <summary>
  24. /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
  25. /// </summary>
  26. /// <returns></returns>
  27. public static T GetInstance()
  28. {
  29. // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
  30. // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
  31. // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
  32. // 双重锁定只需要一句判断就可以了
  33. if (uniqueInstance == null)
  34. {
  35. lock (locker)
  36. {
  37. // 如果类的实例不存在则创建,否则直接返回
  38. if (uniqueInstance == null)
  39. {
  40. try
  41. {
  42. // uniqueInstance = Activator.CreateInstance<T>();
  43. ConstructorInfo constructorInfo = null;
  44. try
  45. {
  46. constructorInfo = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[0], null);
  47. }
  48. catch (Exception ex)
  49. {
  50. throw new InvalidOperationException(ex.Message, ex);
  51. }
  52. if (constructorInfo == null || constructorInfo.IsAssembly)
  53. {
  54. throw new InvalidOperationException($"在'{typeof(T).Name}'里面没有找到private或者protected的构造函数。");
  55. }
  56. uniqueInstance = (T)constructorInfo.Invoke(null);
  57. }
  58. catch (Exception)
  59. {
  60. throw;
  61. }
  62. }
  63. }
  64. }
  65. return uniqueInstance;
  66. }
  67. public static T GetInstance(object[] para)
  68. {
  69. // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
  70. // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
  71. // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
  72. // 双重锁定只需要一句判断就可以了
  73. if (uniqueInstance == null)
  74. {
  75. lock (locker)
  76. {
  77. // 如果类的实例不存在则创建,否则直接返回
  78. if (uniqueInstance == null)
  79. {
  80. try
  81. {
  82. // uniqueInstance = Activator.CreateInstance<T>();
  83. ConstructorInfo constructorInfo = null;
  84. try
  85. {
  86. constructorInfo = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[1] { para[0].GetType() }, null);
  87. }
  88. catch (Exception ex)
  89. {
  90. throw new InvalidOperationException(ex.Message, ex);
  91. }
  92. if (constructorInfo == null || constructorInfo.IsAssembly)
  93. {
  94. throw new InvalidOperationException($"在'{typeof(T).Name}'里面没有找到private或者protected的构造函数。");
  95. }
  96. uniqueInstance = (T)constructorInfo.Invoke(para);
  97. }
  98. catch (Exception)
  99. {
  100. throw;
  101. }
  102. }
  103. }
  104. }
  105. return uniqueInstance;
  106. }
  107. }
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. public sealed class Singleton4<T> where T : class,new ()
  112. {
  113. private static readonly Singleton4<T> instance = new Singleton4<T>();
  114. /// <summary>
  115. /// 显式的静态构造函数用来告诉C#编译器在其内容实例化之前不要标记其类型
  116. /// </summary>
  117. static Singleton4() { }
  118. private Singleton4() { }
  119. public static Singleton4<T> Instance
  120. {
  121. get
  122. {
  123. return instance;
  124. }
  125. }
  126. }
  127. }