|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections.Concurrent;
- using System.Reflection.Metadata;
- using System.Threading;
- using System.Reflection;
-
-
- 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);
- }
-
- public async void SendAsync(string key, object par, Action Callback = null)
- {
- await Task.Run(new Action(() =>
- {
- 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);
- }
-
- public async void SendAsync(string key, object[] par, Action Callback = null)
- {
- await Task.Run(new Action(() =>
- {
- 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 async void SendAsync(string key, Action Callback = null)
- {
- await Task.Run(new Action(() =>
- {
- 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)
- {
- return actions[key].FuncObj?.Invoke();
- }
- else
- {
- return actions[key].FuncPar?.Invoke(par);
- }
- return default;
- }
-
- public async Task<object> SendResultAsync(string key, object par = null)
- {
- return await Task.Run(new Func<object>(() =>
- {
- if (actions.ContainsKey(key))
- if (par == null)
- {
- return actions[key].FuncObj?.Invoke();
- }
- else
- {
- return actions[key].FuncPar?.Invoke(par);
- }
- return default;
- }));
- }
-
- /// <summary>
- /// 根据 Key 注册需要执行的委托
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="action">委托方法</param>
- /// <param name="key">委托key</param>
- /// <param name="IsAutoCancelRegister">是否自动取消注册</param>
- public void Register<T>(T action, string key, bool IsAutoCancelRegister = false)
- {
- lock (RegisterLock)
- {
- if (action != null)
- {
- if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
- try
- {
- 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 });
- }
- }
- catch (Exception ex)
- {
- Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString());
- }
-
- }
- }
-
- }
-
- public async void RegisterAsync<T>(T action, string key, bool IsAutoCancelRegister = false)
- {
- await Task.Run(new Action(() =>
- {
- if (action != null)
- {
- if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
- try
- {
- 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 });
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
-
- }
- }));
- }
-
- #region 升级版本
-
- //private static ConcurrentDictionary<string, meth> MethodS = new ConcurrentDictionary<string, MethodBase>();
- //public void Register1<TPar>(Action<TPar> action, string key, bool IsAutoCancelRegister = false)
- //{
- // MethodS.TryAdd("aa",new Action(() => { }));
- // lock (RegisterLock)
- // {
- // if (action != null)
- // {
- // if (IsAutoCancelRegister && actions.ContainsKey(key)) actions.TryRemove(key, out Delegation d);
- // try
- // {
- // if (!actions.ContainsKey(key))
- // {
- // if (action is Action actionBus)
- // actions.TryAdd(key, new Delegation() { ActionBus = actionBus });
-
- // if (action is Action<TPar> 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 });
- // }
- // }
- // catch (Exception ex)
- // {
- // Message.MessageLog.GetInstance.ShowDebugLog(ex.ToString());
- // }
-
- // }
- // }
-
- //}
-
- #endregion
-
-
-
- }
-
-
- 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; }
- }
-
-
- }
|