3 using System.Collections.Generic;
4 using System.ComponentModel;
6 using System.Reflection;
7 using System.Runtime.Serialization;
11 namespace SharpLib.Ear
14 /// Event Action Router (Ear) is a generic and extensible framework allowing users to execute actions in response to events.
15 /// Users can implement their own events and actions.
18 public class Manager: Object
21 /// Our events instances.
24 public List<Event> Events;
28 /// Executes after internalization took place.
30 protected override void DoConstruct()
36 Events = new List<Event>();
39 // TODO: Object properties should be constructed too
40 foreach (Event e in Events)
48 /// Trigger the given event.
50 /// <param name="aEventType"></param>
51 public async void TriggerEvents<T>() where T: class
53 //Only trigger enabled events matching the desired type
54 foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
61 /// Trigger the given event.
63 /// <param name="aEventType"></param>
64 public async void TriggerEvents<T>(T aEvent) where T : class
66 //Only trigger events matching the desired type
67 foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled))
76 /// <typeparam name="T"></typeparam>
77 /// <param name="aEvent"></param>
78 public async void TriggerEventsByName(string aName)
80 if (string.IsNullOrEmpty(aName))
82 //Just don't do that that would be silly
85 //Only trigger events matching the desired type
86 foreach (Event e in Events.Where(e => e.Name.Equals(aName)))
96 /// Remove the specified action from the event it belongs too.
98 /// <param name="aAction"></param>
99 public void RemoveAction(Action aAction)
101 foreach (Event e in Events)
103 if (e.Actions.Remove(aAction))
105 //We removed our action, we are done here.