12345678910111213141516171819202122232425262728293031323334353637 |
- using System;
- using System.Collections.Generic;
- namespace ET
- {
- public class EventDispatcher<A>
- {
- private Dictionary<Type, List<Action<A>>> _list = new();
- public EventDispatcher() { }
- public Action<A> AddListener<T>(Action<A> action)
- {
- List<Action<A>> acts;
- if (_list.TryGetValue(typeof(T), out acts))
- {
- acts.Add(action);
- }
- else
- {
- _list.Add(typeof(T), new List<Action<A>> { action });
- }
- return action;
- }
- public void Notfify(A ev)
- {
- List<Action<A>> acts;
- if (_list.TryGetValue(ev.GetType(), out acts))
- {
- foreach (var act in acts)
- {
- act.Invoke(ev);
- }
- }
- }
- }
- }
|