Messager.cs 1010 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Concurrent;
  3. namespace SHJX.Service.Common.UserDelegate
  4. {
  5. public static class Messager
  6. {
  7. private static readonly ConcurrentDictionary<string, Action<object>> Actions = new();
  8. /// <summary>
  9. /// 注册
  10. /// </summary>
  11. /// <param name="deleName"></param>
  12. /// <param name="action"></param>
  13. public static void Register(string deleName, Action<object> action)
  14. {
  15. if (!Actions.ContainsKey(deleName))
  16. {
  17. Actions.TryAdd(deleName, action);
  18. }
  19. Actions[deleName] = action;
  20. }
  21. /// <summary>
  22. /// 发送
  23. /// </summary>
  24. /// <param name="opKey"></param>
  25. /// <param name="arg"></param>
  26. public static void Send(string opKey, object arg = null)
  27. {
  28. if (Actions.TryGetValue(opKey, out var service))
  29. {
  30. service?.Invoke(arg);
  31. }
  32. }
  33. }
  34. }