-, #, - ? , ?
, :
class A {
public event EventHandler SomeEvent;
}
class B {
public B(A a) {
a.SomeEvent += (sender, e) => { Console.WriteLine("B handler"); };
}
}
class C {
public C(A a) {
a.SomeEvent += (sender, e) => { Console.WriteLine("C handler"); };
}
}
, ?
, . EventHandlers, , (. Delegate.GetInvocationList). :
class EventHandler {
LinkedList<Action<object, EventArgs>> subscribers =
new LinkedList<Action<object, EventArgs>>();
public void Add(Action<object, EventArgs> f) {
subscribers.AddLast(f);
}
public void Remove(Action<object, EventArgs> f) {
subscribers.Remove(f);
}
public void Invoke(object sender, EventArgs e) {
foreach(Action<object, EventArgs> f in subscribers)
f(sender, e);
}
}
( . , , . / Voodoo.)
, , .