终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ActionManage.cs 4.7 KiB

há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.Concurrent;
  7. namespace BPASmartClient.Helper
  8. {
  9. public class ActionManage
  10. {
  11. private volatile static ActionManage _Instance;
  12. public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
  13. private ActionManage() { }
  14. //private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
  15. private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
  16. static readonly object SendLock = new object();
  17. static readonly object SendParLock = new object();
  18. static readonly object RegisterLock = new object();
  19. /// <summary>
  20. /// 注销委托
  21. /// </summary>
  22. /// <param name="key"></param>
  23. public void CancelRegister(string key)
  24. {
  25. if (actions.ContainsKey(key))
  26. actions.TryRemove(key, out Delegation t);
  27. }
  28. /// <summary>
  29. /// 执行注册过的委托
  30. /// </summary>
  31. /// <param name="key">注册委托的key</param>
  32. /// <param name="par">委托参数</param>
  33. /// <param name="Callback">委托回调</param>
  34. public void Send(string key, object par, Action Callback = null)
  35. {
  36. lock (SendLock)
  37. if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
  38. }
  39. /// <summary>
  40. /// 执行注册过的委托
  41. /// </summary>
  42. /// <param name="key">注册委托的key</param>
  43. /// <param name="par">委托参数</param>
  44. /// <param name="Callback">委托回调</param>
  45. public void Send(string key, object[] par, Action Callback = null)
  46. {
  47. lock (SendLock)
  48. if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
  49. }
  50. /// <summary>
  51. /// 执行注册过的委托
  52. /// </summary>
  53. /// <param name="key">注册委托的key</param>
  54. /// <param name="Callback">委托回调</param>
  55. public void Send(string key, Action Callback = null)
  56. {
  57. lock (SendLock)
  58. if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
  59. }
  60. public object SendResult(string key, object par = null)
  61. {
  62. lock (SendLock)
  63. if (actions.ContainsKey(key))
  64. if (par == null)
  65. {
  66. if (actions[key].FuncObj != null)
  67. return actions[key].FuncObj;
  68. }
  69. else
  70. {
  71. if (actions[key].FuncPar != null)
  72. return actions[key].FuncPar(par);
  73. }
  74. return default;
  75. }
  76. public void Register<T>(T action, string key)
  77. {
  78. lock (RegisterLock)
  79. {
  80. if (action != null)
  81. {
  82. if (!actions.ContainsKey(key))
  83. {
  84. if (action is Action actionBus)
  85. actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
  86. if (action is Action<object> actionObj)
  87. actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
  88. if (action is Action<object[]> actionObjs)
  89. actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
  90. if (action is Func<object> funcObj)
  91. actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
  92. if (action is Func<object, object> puncPar)
  93. actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
  94. }
  95. }
  96. }
  97. }
  98. }
  99. internal class Delegation
  100. {
  101. /// <summary>
  102. /// 带参数的委托
  103. /// </summary>
  104. public Action<object> ActionPar { get; set; }
  105. /// <summary>
  106. /// 带参数的委托
  107. /// </summary>
  108. public Action<object[]> ActionPars { get; set; }
  109. /// <summary>
  110. /// 无参数的委托
  111. /// </summary>
  112. public Action ActionBus { get; set; }
  113. /// <summary>
  114. /// 有返回值的委托
  115. /// </summary>
  116. public Func<object> FuncObj { get; set; }
  117. /// <summary>
  118. /// 有返回值,有参数的委托
  119. /// </summary>
  120. public Func<object, object> FuncPar { get; set; }
  121. }
  122. }