1.定义委托
namespace Framework.Notify
{
public delegate void NotifyCallback();
public delegate void NotifyCallback<T>(T arg);
public delegate void NotifyCallback<T, X>(T arg1, X arg2);
public delegate void NotifyCallback<T, X, Y>(T arg1, X arg2, Y arg3);
public delegate void NotifyCallback<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
}
2.用接口封装事件的方法
namespace Framework.Notify
{
/// <summary>
/// 事件模块接口
/// </summary>
public interface INotifyModule
{
/// <summary>
/// 添加事件监听
/// </summary>
/// <param name="eventName">事件名</param>
/// <param name="callBack">事件回调</param>
void On(string eventName, NotifyCallback callBack);
void On<T>(string eventName, NotifyCallback<T> callBack);
void On<T, X>(string eventName, NotifyCallback<T, X> callBack);
void On<T, X, Y>(string eventName, NotifyCallback<T, X, Y> callBack);
void On<T, X, Y, Z>(string eventName, NotifyCallback<T, X, Y, Z> callBack);
/// <summary>
/// 移除事件监听
/// </summary>
/// <param name="eventName">事件名</param>
/// <param name="callBack">事件回调</param>
void Off(string eventName, NotifyCallback callBack);
void Off<T>(string eventName, NotifyCallback<T> callBack);
void Off<T, X>(string eventName, NotifyCallback<T, X> callBack);
void Off<T, X, Y>(string eventName, NotifyCallback<T, X, Y> callBack);
void Off<T, X, Y, Z>(string eventName, NotifyCallback<T, X, Y, Z> callBack);
/// <summary>
/// 广播事件
/// </summary>
/// <param name="eventName">事件名</param>
void Fire(string eventName);
void Fire<T>(string eventName, T arg);
void Fire<T, X>(string eventName, T arg1, X arg2);
void Fire<T, X, Y>(string eventName, T arg1, X arg2, Y arg3);
void Fire<T, X, Y, Z>(string eventName, T arg1, X arg2, Y arg3, Z arg4);
}
}
3.事件和委托的主逻辑,这是一个多播委托,通过"+"和"-"的操作添加和移除委托
using System;
using System.Collections.Generic;
namespace Framework.Notify
{
/// <summary>
/// 事件模块
/// </summary>
public sealed partial class NotifyModule : INotifyModule
{
private static Dictionary<string, Delegate> m_EventDic = new Dictionary<string, Delegate>();
// 监听事件
public void On(string eventName, NotifyCallback callBack)
{
Create(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback)m_EventDic[eventName] + callBack;
}
public void On<T>(string eventName, NotifyCallback<T> callBack)
{
Create(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T>)m_EventDic[eventName] + callBack;
}
public void On<T, X>(string eventName, NotifyCallback<T, X> callBack)
{
Create(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T, X>)m_EventDic[eventName] + callBack;
}
public void On<T, X, Y>(string eventName, NotifyCallback<T, X, Y> callBack)
{
Create(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T, X, Y>)m_EventDic[eventName] + callBack;
}
public void On<T, X, Y, Z>(string eventName, NotifyCallback<T, X, Y, Z> callBack)
{
Create(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T, X, Y, Z>)m_EventDic[eventName] + callBack;
}
// 移除事件
public void Off(string eventName, NotifyCallback callBack)
{
RemoveCheck(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback)m_EventDic[eventName] - callBack;
Remove(eventName);
}
public void Off<T>(string eventName, NotifyCallback<T> callBack)
{
RemoveCheck(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T>)m_EventDic[eventName] - callBack;
Remove(eventName);
}
public void Off<T, X>(string eventName, NotifyCallback<T, X> callBack)
{
RemoveCheck(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T, X>)m_EventDic[eventName] - callBack;
Remove(eventName);
}
public void Off<T, X, Y>(string eventName, NotifyCallback<T, X, Y> callBack)
{
RemoveCheck(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T, X, Y>)m_EventDic[eventName] - callBack;
Remove(eventName);
}
public void Off<T, X, Y, Z>(string eventName, NotifyCallback<T, X, Y, Z> callBack)
{
RemoveCheck(eventName, callBack);
m_EventDic[eventName] = (NotifyCallback<T, X, Y, Z>)m_EventDic[eventName] - callBack;
Remove(eventName);
}
// 广播事件
public void Fire(string eventName)
{
Delegate d;
if (m_EventDic.TryGetValue(eventName, out d))
{
NotifyCallback callBack = d as NotifyCallback;
if (callBack != null)
{
callBack.Invoke();
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventName));
}
}
}
public void Fire<T>(string eventName, T arg)
{
Delegate d;
if (m_EventDic.TryGetValue(eventName, out d))
{
NotifyCallback<T> callBack = d as NotifyCallback<T>;
if (callBack != null)
{
callBack.Invoke(arg);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventName));
}
}
}
public void Fire<T, X>(string eventName, T arg1, X arg2)
{
Delegate d;
if (m_EventDic.TryGetValue(eventName, out d))
{
NotifyCallback<T, X> callBack = d as NotifyCallback<T, X>;
if (callBack != null)
{
callBack.Invoke(arg1, arg2);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventName));
}
}
}
public void Fire<T, X, Y>(string eventName, T arg1, X arg2, Y arg3)
{
Delegate d;
if (m_EventDic.TryGetValue(eventName, out d))
{
NotifyCallback<T, X, Y> callBack = d as NotifyCallback<T, X, Y>;
if (callBack != null)
{
callBack.Invoke(arg1, arg2, arg3);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventName));
}
}
}
public void Fire<T, X, Y, Z>(string eventName, T arg1, X arg2, Y arg3, Z arg4)
{
Delegate d;
if (m_EventDic.TryGetValue(eventName, out d))
{
NotifyCallback<T, X, Y, Z> callBack = d as NotifyCallback<T, X, Y, Z>;
if (callBack != null)
{
callBack.Invoke(arg1, arg2, arg3, arg4);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventName));
}
}
}
/// <summary>
/// 添加事件
/// </summary>
/// <param name="eventName">事件名</param>
/// <param name="callBack">事件回调</param>
private static void Create(string eventName, Delegate callBack)
{
if (!m_EventDic.ContainsKey(eventName))
{
m_EventDic.Add(eventName, null);
}
Delegate d = m_EventDic[eventName];
if (d != null && d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventName, d.GetType(), callBack.GetType()));
}
}
private static void RemoveCheck(string eventName, Delegate callBack)
{
if (m_EventDic.ContainsKey(eventName))
{
Delegate d = m_EventDic[eventName];
if (d == null)
{
throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventName));
}
else if (d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventName, d.GetType(), callBack.GetType()));
}
}
else
{
throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventName));
}
}
private static void Remove(string eventName)
{
if (m_EventDic[eventName] == null)
{
m_EventDic.Remove(eventName);
}
}
}
}