实现带返回参数的事件收发机制
写了一个事件通知模型,能直接拿去用,这个版本还是比较完善的
测试代码如下:
class Program { static void Main(string[] args) { Func<string> testFunction = () => { return "Test"; }; EventManager.RegisterEvent<string>(EventTable.TEST_01, testFunction); string value = EventManager.SendEventHasReturn<string>(EventTable.TEST_01); Console.WriteLine(value); Action<string> testAction = (string a) => { Console.WriteLine(a); }; EventManager.RegisterEvent<string>(EventTable.TEST_01, testAction); EventManager.SendEvent<string>(EventTable.TEST_01,"Test2"); Console.ReadKey(); } }
运行结果如下
源码如下
/* * EventManager.cs * 事件机制。 * * 注册事件:RegisterEvent * 取消注册:UnregisterEvent * 触发事件:SendEvent * 用法参考:自己看注释 * * */ using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class EventManager { private static Dictionary<Type, Dictionary<string, Delegate>> eventTable; public delegate void Action<T, U, V, W, X>(T arg1, U arg2, V arg3, W arg4, X arg5); /// <summary> /// 作为具有返回值类型的委托试用 /// </summary> public delegate TResult Func<in T, in U, in V, in W, in X, out TResult>(T arg1, U arg2, V arg3, W arg4, X arg5); //注册事件 private static void _RegisterEvent(string name, Delegate handler) { if (eventTable == null) { eventTable = new Dictionary<Type, Dictionary<string, Delegate>>(); } Dictionary<string, Delegate> dictionary; if (!eventTable.TryGetValue(handler.GetType(), out dictionary)) { dictionary = new Dictionary<string, Delegate>(); eventTable.Add(handler.GetType(), dictionary); } Delegate a; if (dictionary.TryGetValue(name, out a)) { dictionary[name] = Delegate.Combine(a, handler); } else { dictionary.Add(name, handler); } } /// <summary> /// 注册事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent(string name, System.Action handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<TResult>(string name, System.Func<TResult> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<TResult>(EventTable name, System.Func<TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T>(string name, Action<T> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, TResult>(string name, System.Func<T, TResult> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, TResult>(EventTable name, System.Func<T, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U>(string name, Action<T, U> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, TResult>(string name, System.Func<T, U, TResult> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, TResult>(EventTable name, System.Func<T, U, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V>(string name, Action<T, U, V> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, TResult>(string name, System.Func<T, U, V, TResult> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, TResult>(EventTable name, System.Func<T, U, V, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W>(string name, Action<T, U, V, W> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, TResult>(string name, System.Func<T, U, V, W, TResult> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, TResult>(EventTable name, System.Func<T, U, V, W, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, X>(string name, Action<T, U, V, W, X> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, X, TResult>(string name, Func<T, U, V, W, X, TResult> handler) { _RegisterEvent(name, handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, X, TResult>(EventTable name, Func<T, U, V, W, X, TResult> handler) { _RegisterEvent(name.ToString(), handler); } private static Delegate GetDelegate(string name, Type type) { Dictionary<string, Delegate> dictionary; Delegate result; if (eventTable != null && eventTable.TryGetValue(type, out dictionary) && dictionary.TryGetValue(name, out result)) { return result; } return null; } /// <summary> /// 调用注册事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent(string name) { System.Action action = GetDelegate(name, typeof(System.Action)) as System.Action; if (action != null) { action(); } } /// <summary> /// 调用注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T>(string name, T arg1) { Action<T> action = GetDelegate(name, typeof(Action<T>)) as Action<T>; if (action != null) { action(arg1); } } /// <summary> /// 调用注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T, U>(string name, T arg1, U arg2) { Action<T, U> action = GetDelegate(name, typeof(Action<T, U>)) as Action<T, U>; if (action != null) { action(arg1, arg2); } } /// <summary> /// 调用注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T, U, V>(string name, T arg1, U arg2, V arg3) { Action<T, U, V> action = GetDelegate(name, typeof(Action<T, U, V>)) as Action<T, U, V>; if (action != null) { action(arg1, arg2, arg3); } } /// <summary> /// 调用注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T, U, V, W>(string name, T arg1, U arg2, V arg3, W arg4) { Action<T, U, V, W> action = GetDelegate(name, typeof(Action<T, U, V, W>)) as Action<T, U, V, W>; if (action != null) { action(arg1, arg2, arg3, arg4); } } /// <summary> /// 调用注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T, U, V, W, X>(string name, T arg1, U arg2, V arg3, W arg4, X arg5) { Action<T, U, V, W, X> action = GetDelegate(name, typeof(Action<T, U, V, W, X>)) as Action<T, U, V, W, X>; if (action != null) { action(arg1, arg2, arg3, arg4, arg5); } } private static void _UnregisterEvent(string name, Delegate handler) { if (eventTable == null) return; Dictionary<string, Delegate> dictionary; Delegate source; if (eventTable.TryGetValue(handler.GetType(), out dictionary) && dictionary.TryGetValue(name, out source)) { dictionary[name] = Delegate.Remove(source, handler); } } /// <summary> /// 取消注册事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent(string name, System.Action handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<TResult>(string name, System.Func<TResult> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T>(string name, Action<T> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, TResult>(string name, System.Func<T, TResult> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U>(string name, Action<T, U> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, TResult>(string name, System.Func<T, U, TResult> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V>(string name, Action<T, U, V> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, TResult>(string name, System.Func<T, U, V, TResult> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, W>(string name, Action<T, U, V, W> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, W, TResult>(string name, System.Func<T, U, V, W, TResult> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, W, X>(string name, Action<T, U, V, W, X> handler) { _UnregisterEvent(name, handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, W, X, TResult>(string name, Func<T, U, V, W, X, TResult> handler) { _UnregisterEvent(name, handler); } #region Extend to Support EventTable.cs /// <summary> /// 注册事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent(EventTable name, System.Action handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<TResult>(EventTable name, System.Func<TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T>(EventTable name, Action<T> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, TResult>(EventTable name, System.Func<T, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U>(EventTable name, Action<T, U> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, TResult>(EventTable name, System.Func<T, U, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V>(EventTable name, Action<T, U, V> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, TResult>(EventTable name, System.Func<T, U, V, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W>(EventTable name, Action<T, U, V, W> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, TResult>(EventTable name, System.Func<T, U, V, W, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, X>(EventTable name, Action<T, U, V, W, X> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void RegisterEvent<T, U, V, W, X, TResult>(EventTable name, Func<T, U, V, W, X, TResult> handler) { _RegisterEvent(name.ToString(), handler); } /// <summary> /// 取消注册事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent(EventTable name, System.Action handler) { _UnregisterEvent(name.ToString(), handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T>(EventTable name, Action<T> handler) { _UnregisterEvent(name.ToString(), handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U>(EventTable name, Action<T, U> handler) { _UnregisterEvent(name.ToString(), handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V>(EventTable name, Action<T, U, V> handler) { _UnregisterEvent(name.ToString(), handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, W>(EventTable name, Action<T, U, V, W> handler) { _UnregisterEvent(name.ToString(), handler); } /// <summary> /// 取消注册事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="W">第四个参数类型</typeparam> /// <typeparam name="X">第五个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void UnregisterEvent<T, U, V, W, X>(EventTable name, Action<T, U, V, W, X> handler) { _UnregisterEvent(name.ToString(), handler); } /// <summary> /// 调用事件 /// </summary> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent(EventTable name) { System.Action action = GetDelegate(name.ToString(), typeof(System.Action)) as System.Action; if (action != null) { action(); } } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<TResult>(EventTable name) { System.Func<TResult> func = GetDelegate(name.ToString(), typeof(System.Func<TResult>)) as System.Func<TResult>; if (func != null) { return func(); } return default(TResult); } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<TResult>(string name) { System.Func<TResult> func = GetDelegate(name, typeof(System.Func<TResult>)) as System.Func<TResult>; if (func != null) { return func(); } return default(TResult); } /// <summary> /// 调用事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T>(EventTable name, T arg1) { Action<T> action = GetDelegate(name.ToString(), typeof(Action<T>)) as Action<T>; if (action != null) { action(arg1); } } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<T, TResult>(EventTable name, T arg1) { System.Func<T, TResult> func = GetDelegate(name.ToString(), typeof(System.Func<T, TResult>)) as System.Func<T, TResult>; if (func != null) { return func(arg1); } return default(TResult); } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<T, TResult>(string name, T arg1) { System.Func<T, TResult> func = GetDelegate(name, typeof(System.Func<T, TResult>)) as System.Func<T, TResult>; if (func != null) { return func(arg1); } return default(TResult); } /// <summary> /// 调用事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T, U>(EventTable name, T arg1, U arg2) { Action<T, U> action = GetDelegate(name.ToString(), typeof(Action<T, U>)) as Action<T, U>; if (action != null) { action(arg1, arg2); } } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<T, U, TResult>(EventTable name, T arg1, U arg2) { System.Func<T, U, TResult> func = GetDelegate(name.ToString(), typeof(System.Func<T, U, TResult>)) as System.Func<T, U, TResult>; if (func != null) { return func(arg1, arg2); } return default(TResult); } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<T, U, TResult>(string name, T arg1, U arg2) { System.Func<T, U, TResult> func = GetDelegate(name, typeof(System.Func<T, U, TResult>)) as System.Func<T, U, TResult>; if (func != null) { return func(arg1, arg2); } return default(TResult); } /// <summary> /// 调用事件 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static void SendEvent<T, U, V>(EventTable name, T arg1, U arg2, V arg3) { Action<T, U, V> action = GetDelegate(name.ToString(), typeof(Action<T, U, V>)) as Action<T, U, V>; if (action != null) { action(arg1, arg2, arg3); } } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name="V">第三个参数类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="name">标识</param> /// <param name="handler">方法</param> public static TResult SendEventHasReturn<T, U, V, TResult>(EventTable name, T arg1, U arg2, V arg3) { System.Func<T, U, V, TResult> func = GetDelegate(name.ToString(), typeof(System.Func<T, U, V, TResult>)) as System.Func<T, U, V, TResult>; if (func != null) { return func(arg1, arg2, arg3); } return default(TResult); } /// <summary> /// 调用带返回值的方法 /// </summary> /// <typeparam name="T">第一个参数类型</typeparam> /// <typeparam name="U">第二个参数类型</typeparam> /// <typeparam name=
-----------------------------------------------------------
转载请注明出处,本博客文章归作者与博客园所有 。 Keyle.cnblogs.com
-----------------------------------------------------------