代码
namespace EventSystem
{
public delegate void Callback();
public delegate void Callback<T>(T arg);
public delegate void Callback<T, X>(T arg1, X arg2);
public delegate void Callback<T, X, Y>(T arg1, X arg2, Y arg3);
public delegate void Callback<T, X, Y, Z>(T arg1, X arg2,Y arg3,Z arg4);
public delegate void Callback<T, X, Y, Z, W>(T arg1, X arg2,Y arg3, Z arg4, W arg5);
}
namespace EventSystem
{
public enum EventType
{
Update
}
}
using System;
using System.Collections.Generic;
namespace EventSystem
{
public class EventCenter
{
private static readonly Dictionary<EventType, Delegate> m_EventMap = new Dictionary<EventType, Delegate>();
#region 添加监听
public static void AddListener(EventType type, Callback callback)
{
DoAddListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback) + callback;
}
public static void AddListener<T>(EventType type, Callback<T> callback)
{
DoAddListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T>) + callback;
}
public static void AddListener<T, X>(EventType type, Callback<T, X> callback)
{
DoAddListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X>) + callback;
}
public static void AddListener<T, X, Y>(EventType type, Callback<T, X, Y> callback)
{
DoAddListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X, Y>) + callback;
}
public static void AddListener<T, X, Y, Z>(EventType type, Callback<T, X, Y, Z> callback)
{
DoAddListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X, Y, Z>) + callback;
}
public static void AddListener<T, X, Y, Z, W>(EventType type, Callback<T, X, Y, Z, W> callback)
{
DoAddListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X, Y, Z, W>) + callback;
}
#endregion
#region 移除监听
public static void RemoveListener(EventType type, Callback callback)
{
DoRemoveListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback) - callback;
if (m_EventMap[type] == null) m_EventMap.Remove(type);
}
public static void RemoveListener<T>(EventType type, Callback<T> callback)
{
DoRemoveListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T>) - callback;
if (m_EventMap[type] == null) m_EventMap.Remove(type);
}
public static void RemoveListener<T, X>(EventType type, Callback<T, X> callback)
{
DoRemoveListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X>) - callback;
if (m_EventMap[type] == null) m_EventMap.Remove(type);
}
public static void RemoveListener<T, X, Y>(EventType type, Callback<T, X, Y> callback)
{
DoRemoveListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X, Y>) - callback;
if (m_EventMap[type] == null) m_EventMap.Remove(type);
}
public static void RemoveListener<T, X, Y, Z>(EventType type, Callback<T, X, Y, Z> callback)
{
DoRemoveListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X, Y, Z>) - callback;
if (m_EventMap[type] == null) m_EventMap.Remove(type);
}
public static void RemoveListener<T, X, Y, Z, W>(EventType type, Callback<T, X, Y, Z, W> callback)
{
DoRemoveListenerCheck(type, callback);
m_EventMap[type] = (m_EventMap[type] as Callback<T, X, Y, Z, W>) - callback;
if (m_EventMap[type] == null) m_EventMap.Remove(type);
}
#endregion
#region 广播
public static void Broadcast(EventType type)
{
Delegate del = null;
if (m_EventMap.TryGetValue(type, out del))
{
Callback callback = (del as Callback);
if (callback != null)
callback();
}
else
throw new Exception("事件广播错误!EventType=" + type);
}
public static void Broadcast<T>(EventType type, T arg)
{
Delegate del = null;
if (m_EventMap.TryGetValue(type, out del))
{
Callback<T> callback = (del as Callback<T>);
if (callback != null)
callback(arg);
}
else
throw new Exception("事件广播错误!EventType=" + type);
}
public static void Broadcast<T, X>(EventType type, T arg1, X arg2)
{
Delegate del = null;
if (m_EventMap.TryGetValue(type, out del))
{
Callback<T, X> callback = (del as Callback<T, X>);
if (callback != null)
callback(arg1, arg2);
}
else
throw new Exception("事件广播错误!EventType=" + type);
}
public static void Broadcast<T, X, Y>(EventType type, T arg1, X arg2, Y arg3)
{
Delegate del = null;
if (m_EventMap.TryGetValue(type, out del))
{
Callback<T, X, Y> callback = (del as Callback<T, X, Y>);
if (callback != null)
callback(arg1, arg2, arg3);
}
else
throw new Exception("事件广播错误!EventType=" + type);
}
public static void Broadcast<T, X, Y, Z>(EventType type, T arg1, X arg2, Y arg3, Z arg4)
{
Delegate del = null;
if (m_EventMap.TryGetValue(type, out del))
{
Callback<T, X, Y, Z> callback = (del as Callback<T, X, Y, Z>);
if (callback != null)
callback(arg1, arg2, arg3, arg4);
}
else
throw new Exception("事件广播错误!EventType=" + type);
}
public static void Broadcast<T, X, Y, Z, W>(EventType type, T arg1, X arg2, Y arg3, Z arg4, W arg5)
{
Delegate del = null;
if (m_EventMap.TryGetValue(type, out del))
{
Callback<T, X, Y, Z, W> callback = (del as Callback<T, X, Y, Z, W>);
if (callback != null)
callback(arg1, arg2, arg3, arg4, arg5);
}
else
throw new Exception("事件广播错误!EventType=" + type);
}
#endregion
#region 私有方法
private static void DoAddListenerCheck(EventType eventType, Delegate callback)
{
if (!m_EventMap.ContainsKey(eventType))
m_EventMap.Add(eventType, null);
Delegate del = m_EventMap[eventType];
if (del != null && del.GetType() != callback.GetType())
throw new Exception("添加监听错误: 委托类型不一致!");
}
private static void DoRemoveListenerCheck(EventType eventType, Delegate callback)
{
if (callback == null) throw new Exception("移除监听错误: Callback为null!");
Delegate del = null;
if (m_EventMap.TryGetValue(eventType, out del))
{
if (del.GetType() != callback.GetType())
throw new Exception("移除监听错误: 委托类型不正确! eventType=" + eventType);
}
else
{
throw new Exception("移除监听错误: 没有对应的事件类型!eventType=" + eventType);
}
}
#endregion
}
}
测试
using System;
namespace EventSystem
{
internal class Program
{
public static void Main(string[] args)
{
EventCenter.AddListener(EventType.Update,Update);
EventCenter.Broadcast(EventType.Update);
Console.ReadKey();
}
public static void Update()
{
Console.WriteLine("Update");
}
}
}
