Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace EventManager2b
- {
- class EventManager
- {
- public delegate void MyEventHandler(object sender, EventArgs e);
- private Dictionary<string, Dummy> listeners = new Dictionary<string, Dummy>();
- public void AttachListener(string myEvent, MyEventHandler handler)
- {
- Dummy dummy;
- if (listeners.ContainsKey(myEvent))
- {
- dummy = listeners[myEvent];
- dummy.handlers += handler;
- }
- else
- {
- dummy = new Dummy();
- dummy.handlers += handler;
- listeners.Add(myEvent, dummy);
- }
- }
- public void TriggerEvent(string myEvent, object sender, EventArgs e)
- {
- if(listeners.ContainsKey(myEvent))
- {
- Dummy dummy = listeners[myEvent];
- dummy.OnEvent(sender, e);
- }
- }
- public class Dummy
- {
- public event MyEventHandler handlers;
- public void OnEvent(object sender, EventArgs e)
- {
- if (handlers != null)
- {
- handlers(sender, e);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement