| 1234567891011121314151617181920212223242526272829303132 |
- using System;
- using System.Collections.Concurrent;
- namespace SHJX.Service.Common.Utils
- {
- public static class Messager<T> where T : class
- {
- private static readonly ConcurrentDictionary<string, Action<T>> Actions = new();
- /// <summary>
- /// 注册消息
- /// </summary>
- public static void Register(string name, Action<T> action)
- {
- if (!Actions.ContainsKey(name))
- {
- Actions.TryAdd(name, action);
- }
- Actions[name] = action;
- }
- /// <summary>
- /// 消息发送
- /// </summary>
- public static void Send(string name, T t = null)
- {
- if (Actions.TryGetValue(name, out Action<T> service))
- {
- service?.Invoke(t);
- }
- }
- }
- }
|