using System;
using System.Collections;
public class EventSystem {
public delegate void ListenerDelegate();
public delegate void ListenerDelegate<T>(T t);
public delegate void ListenerDelegate<T1, T2>(T1 t1, T2 t2);
public delegate void ListenerDelegate<T1, T2, T3>(T1 t1, T2 t2, T3 t3);
private Hashtable listeners = new Hashtable();
#region Addlistener
public void AddListener(string eventName, ListenerDelegate listener) {
ListenerDelegate listenerDelegate = listeners[eventName] as ListenerDelegate;
listenerDelegate = (ListenerDelegate)Delegate.Combine(listenerDelegate, listener);
listeners[eventName] = listenerDelegate;
}
public void AddListener<T>(string eventName, ListenerDelegate<T> listener) {
ListenerDelegate<T> listenerDelegate = listeners[eventName] as ListenerDelegate<T>;
listenerDelegate = (ListenerDelegate<T>)Delegate.Combine(listenerDelegate, listener);
listeners[eventName] = listenerDelegate;
}
public void AddListener<T1,T2>(string eventName, ListenerDelegate<T1,T2> listener) {
ListenerDelegate<T1,T2> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2>;
listenerDelegate = (ListenerDelegate<T1,T2>)Delegate.Combine(listenerDelegate, listener);
listeners[eventName] = listenerDelegate;
}
public void AddListener<T1,T2,T3>(string eventName, ListenerDelegate<T1,T2,T3> listener) {
ListenerDelegate<T1,T2,T3> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2,T3>;
listenerDelegate = (ListenerDelegate<T1,T2,T3>)Delegate.Combine(listenerDelegate, listener);
listeners[eventName] = listenerDelegate;
}
#endregion
#region RemoveListener
public void RemoveListener(string eventName, ListenerDelegate listener) {
Delegate remove = Delegate.Remove(listeners[eventName] as ListenerDelegate, listener);
SetRemoveValue(eventName, remove);
}
public void RemoveListener<T>(string eventName, ListenerDelegate<T> listener) {
var remove = Delegate.Remove(listeners[eventName] as ListenerDelegate<T>, listener);
SetRemoveValue(eventName, remove);
}
public void RemoveListener<T1, T2>(string eventName, ListenerDelegate<T1, T2> listener) {
Delegate remove = Delegate.Remove(listeners[eventName] as ListenerDelegate<T1, T2>, listener);
SetRemoveValue(eventName, remove);
}
public void RemoveListener<T1, T2, T3>(string eventName, ListenerDelegate<T1, T2, T3> listener) {
Delegate remove = Delegate.Remove(listeners[eventName] as ListenerDelegate<T1, T2, T3>, listener);
SetRemoveValue(eventName, remove);
}
private void SetRemoveValue(string eventName, Delegate remove) {
if (remove == null) {
listeners.Remove(eventName);
} else {
listeners[eventName] = remove;
}
}
#endregion
public void RemoveAllListener(string eventName) {
listeners.Remove(eventName);
}
public void Dispatch(string eventName)
{
if (listeners.ContainsKey(eventName)) {
ListenerDelegate listenerDelegate = listeners[eventName] as ListenerDelegate;
listenerDelegate();
}
}
public void Dispatch<T>(string eventName, T t) {
if (listeners.ContainsKey(eventName)) {
ListenerDelegate<T> listenerDelegate = listeners[eventName] as ListenerDelegate<T>;
listenerDelegate(t);
}
}
public void Dispatch<T1, T2>(string eventName, T1 t1, T2 t2) {
if (listeners.ContainsKey(eventName)) {
ListenerDelegate<T1,T2> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2>;
listenerDelegate(t1, t2);
}
}
public void Dispatch<T1, T2, T3>(string eventName, T1 t1, T2 t2, T3 t3) {
if (listeners.ContainsKey(eventName)) {
ListenerDelegate<T1,T2,T3> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2,T3>;
listenerDelegate(t1, t2, t3);
}
}
}
最多支持3个参数,再多我认为要写结构体了
Delegate.Combine和+= 是有区别的 https://www.coder.work/article/1627775