1
//
2
using System;
3
using System.Collections.Generic;
4
using System.ComponentModel;
5
using System.Linq;
6
using System.Reflection;
7
using System.Runtime.Serialization;
8
using SharpLib.Utils;
9
10
11
namespace SharpLib.Ear
12
{
13
/// <summary>
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.
16
/// </summary>
17
[DataContract]
18
[KnownType("DerivedTypes")]
19
public class Manager
20
21
22
/// Our events instances.
23
24
[DataMember]
25
public List<Event> Events;
26
27
28
/// Constructor
29
30
public Manager()
31
32
Init();
33
}
34
35
36
/// Executes after internalization took place.
37
38
public void Init()
39
40
if (Events == null)
41
42
Events = new List<Event>();
43
44
45
46
47
48
/// Trigger the given event.
49
50
/// <param name="aEventType"></param>
51
public void TriggerEvent<T>() where T: class
52
53
//Only trigger enabled events matching the desired type
54
foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled))
55
56
e.Trigger();
57
58
59
60
61
62
/// Remove the specified action from the event it belongs too.
63
64
/// <param name="aAction"></param>
65
public void RemoveAction(Action aAction)
66
67
foreach (Event e in Events)
68
69
if (e.Actions.Remove(aAction))
70
71
//We removed our action, we are done here.
72
return;
73
74
75
76
77
78
/// Allow extending our data contract.
79
/// See KnownType above.
80
81
/// <returns></returns>
82
private static IEnumerable<Type> DerivedTypes()
83
84
return SharpLib.Utils.Reflection.GetDerivedTypes<Manager>();
85
86
87