找回密码
 立即注册
查看: 207|回复: 0

基于c#写一个EventCenter事件管理器

[复制链接]
发表于 2022-9-30 10:40 | 显示全部楼层 |阅读模式
using System.Collections.Generic;using System;namespace EventCenterTool{    public class EventCenter    {        //定义一个字典用来存储委托事件        static Dictionary<string,Delegate> m_EventTable = new Dictionary<string,Delegate>();        static void OnListenerAdding(string str, Delegate callBack)        {            //检查当前字典是否存在事件str,没有就添加一个null事件            if (!m_EventTable.ContainsKey(str))            {                m_EventTable.Add(str, null);            }            Delegate d = m_EventTable[str];            if (d != null && d.GetType() != callBack.GetType())            {                throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件对应的委托类型是{1},需要添加的委托类型是{2}", str, d.GetType(), callBack.GetType()));            }        }        static void OnListenerRemoving(string str, Delegate callBack)        {            if (m_EventTable.ContainsKey(str))            {                Delegate d = m_EventTable[str];                if (d == null)                {                    throw new Exception(string.Format("移除监听错误:当前事件{0}没有对应的委托",str));                }                if (d.GetType() != callBack.GetType())                {                    throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前事件对应的委托类型是{1},需要移除的委托类型是{2}", str, d.GetType(), callBack.GetType()));                }            }            else            {                throw new Exception(string.Format("移除监听错误:没有这个事件{0}",str));            }        }        static void OnListenerRemoved(string str)        {            if (m_EventTable[str] == null)            {                m_EventTable.Remove(str);            }        }        //1个参数添加监听方法        public static void AddListener<T>(string str, CallBack<T> callBack)        {            OnListenerAdding(str, callBack);            m_EventTable[str] = (CallBack<T>)m_EventTable[str] + callBack;        }        ////2个参数移除监听方法        public static void RemoveListener<T,X>(string str, CallBack<T,X> callBack)        {            OnListenerRemoving(str, callBack);            m_EventTable[str] = (CallBack<T, X>)m_EventTable[str] - callBack;            OnListenerRemoved(str);        }        //无参数广播监听方法        public static void BroadCast(string str)        {            Delegate d;#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。            if (m_EventTable.TryGetValue(str,out d))#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。            {                CallBack? callBack = d as CallBack;                if (callBack == null)                {                    throw new Exception(string.Format("广播事件错误:当前事件{0}具有不同类型的委托", str));                }                else callBack();            }        }    }}public delegate void CallBack();public delegate void CallBack<T>(T arg1);public delegate void CallBack<T, X>(T arg1, X arg2);
上面我分别写了无参、1个参数、两个参数的监听,如果需要其它参数的情况自己按照例子添加就行了,感谢观看!!!
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-7-4 09:12 , Processed in 0.089452 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表