Advertisement
wingman007

C#EventManager_EventManager_2b

Oct 7th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace EventManager2b
  8. {
  9.     class EventManager
  10.     {
  11.         public delegate void MyEventHandler(object sender, EventArgs e);
  12.  
  13.         private Dictionary<string, Dummy> listeners = new Dictionary<string, Dummy>();
  14.  
  15.         public void AttachListener(string myEvent, MyEventHandler handler)
  16.         {
  17.             Dummy dummy;
  18.             if (listeners.ContainsKey(myEvent))
  19.             {
  20.                 dummy = listeners[myEvent];
  21.                 dummy.handlers += handler;
  22.             }
  23.             else
  24.             {
  25.                 dummy = new Dummy();
  26.                 dummy.handlers += handler;
  27.                 listeners.Add(myEvent, dummy);
  28.             }
  29.         }
  30.  
  31.         public void TriggerEvent(string myEvent, object sender, EventArgs e)
  32.         {
  33.             if(listeners.ContainsKey(myEvent))
  34.             {
  35.                 Dummy dummy = listeners[myEvent];
  36.                 dummy.OnEvent(sender, e);
  37.             }
  38.         }
  39.  
  40.         public class Dummy
  41.         {
  42.             public event MyEventHandler handlers;
  43.  
  44.             public void OnEvent(object sender, EventArgs e)
  45.             {
  46.                 if (handlers != null)
  47.                 {
  48.                     handlers(sender, e);
  49.                 }
  50.             }
  51.         }
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement