|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections.Concurrent;
-
- namespace BPASmartClient.Helper
- {
- public class ActionManage
- {
-
- private volatile static ActionManage _Instance;
- public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
- private ActionManage() { }
-
- //private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
- private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();
-
- static readonly object SendLock = new object();
- static readonly object SendParLock = new object();
- static readonly object RegisterLock = new object();
-
- /// <summary>
- /// 注销委托
- /// </summary>
- /// <param name="key"></param>
- public void CancelRegister(string key)
- {
- if (actions.ContainsKey(key))
- actions.TryRemove(key, out Delegation t);
- }
-
- /// <summary>
- /// 执行注册过的委托
- /// </summary>
- /// <param name="key">注册委托的key</param>
- /// <param name="par">委托参数</param>
- /// <param name="Callback">委托回调</param>
- public void Send(string key, object par, Action Callback = null)
- {
- lock (SendLock)
- if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
- }
-
- /// <summary>
- /// 执行注册过的委托
- /// </summary>
- /// <param name="key">注册委托的key</param>
- /// <param name="par">委托参数</param>
- /// <param name="Callback">委托回调</param>
- public void Send(string key, object[] par, Action Callback = null)
- {
- lock (SendLock)
- if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
- }
-
- /// <summary>
- /// 执行注册过的委托
- /// </summary>
- /// <param name="key">注册委托的key</param>
- /// <param name="Callback">委托回调</param>
- public void Send(string key, Action Callback = null)
- {
- lock (SendLock)
- if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
- }
-
- public object SendResult(string key, object par = null)
- {
- lock (SendLock)
- if (actions.ContainsKey(key))
- if (par == null)
- {
- if (actions[key].FuncObj != null)
- return actions[key].FuncObj;
- }
- else
- {
- if (actions[key].FuncPar != null)
- return actions[key].FuncPar(par);
- }
- return default;
- }
-
- public void Register<T>(T action, string key)
- {
- lock (RegisterLock)
- {
- if (action != null)
- {
- if (!actions.ContainsKey(key))
- {
- if (action is Action actionBus)
- actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
-
- if (action is Action<object> actionObj)
- actions.TryAdd(key, new Delegation() { ActionPar = actionObj });
-
- if (action is Action<object[]> actionObjs)
- actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });
-
- if (action is Func<object> funcObj)
- actions.TryAdd(key, new Delegation() { FuncObj = funcObj });
-
- if (action is Func<object, object> puncPar)
- actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
- }
- }
- }
-
- }
-
- }
-
-
- internal class Delegation
- {
- /// <summary>
- /// 带参数的委托
- /// </summary>
- public Action<object> ActionPar { get; set; }
- /// <summary>
- /// 带参数的委托
- /// </summary>
- public Action<object[]> ActionPars { get; set; }
- /// <summary>
- /// 无参数的委托
- /// </summary>
- public Action ActionBus { get; set; }
- /// <summary>
- /// 有返回值的委托
- /// </summary>
- public Func<object> FuncObj { get; set; }
- /// <summary>
- /// 有返回值,有参数的委托
- /// </summary>
- public Func<object, object> FuncPar { get; set; }
- }
-
-
- }
|