I´ve prepared a complete example. You can use it in this way:
eventsSubscriptions["1"].EventHandler = new EventHandler(this.Method1);
eventsSubscriptions["2"].EventHandler = new EventHandler(this.Method2);
eventsSubscriptions["3"].EventHandler = new EventHandler(this.Method3);
Boss Boss1 = new Boss("John Smith");
Boss Boss2 = new Boss("Cynthia Jameson");
Employed Employed1 = new Employed("David Ryle");
Employed Employed2 = new Employed("Samantha Sun");
Employed Employed3 = new Employed("Dick Banshee");
// Subscribe objects to Method 1
eventsSubscriptions["1"].Subscribe(Boss1);
eventsSubscriptions["1"].Subscribe(Employed1);
// Subscribe objects to Method 2
eventsSubscriptions["2"].Subscribe(Boss2);
eventsSubscriptions["2"].Subscribe(Employed2);
// Subscribe objects to Method 3
eventsSubscriptions["3"].Subscribe(Employed3);
Then, you can call RaiseAllEvents() methods, and this is the console output:
- Method 1 raised with Boss John Smith
- Method 1 raised with Employee David Ryle
- Method 2 raised with Boss Cynthia Jameson
- Method 2 raised with Employee Samantha Sun
- Method 3 raised with Employee Dick Banshee
In the following lines, I´ll paste the code of all the classes involved. With a little patience and copy/paste, you´ll be able to test it =P Hope it helps you.
--- The Code ---
Main
namespace MyExample
{
public class Program
{
static void Main(string[] args)
{
SomeExampleClass someExampleInstance = new SomeExampleClass();
someExampleInstance.SuscribeObjects();
someExampleInstance.RaiseAllEvents();
Console.ReadLine();
}
}
}
Class Person
namespace MyExample
{
public abstract class Person
{
protected string name;
public Person(string name)
{
this.name = name;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public override string ToString()
{
return (this.GetType().Name + " " + name);
}
}
}
Class Boss
namespace MyExample
{
public class Boss : Person
{
public Boss(string name)
: base(name)
{ }
}
}
Employee
namespace MyExample
{
public class Employee : Person
{
public Employee(string name)
: base(name)
{ }
}
}
Class SomeExampleClass
namespace MyExample
{
public class SomeExampleClass
{
private EventsSubscriptions eventsSubscriptions = new EventsSubscriptions();
private void Method1(object sender, System.EventArgs e)
{
Console.WriteLine("Method 1 raised with " + sender.ToString());
}
private void Method2(object sender, System.EventArgs e)
{
Console.WriteLine("Method 2 raised with " + sender.ToString());
}
private void Method3(object sender, System.EventArgs e)
{
Console.WriteLine("Method 3 raised with " + sender.ToString());
}
public void SuscribeObjects()
{
eventsSubscriptions["1"].EventHandler = new EventHandler(this.Method1);
eventsSubscriptions["2"].EventHandler = new EventHandler(this.Method2);
eventsSubscriptions["3"].EventHandler = new EventHandler(this.Method3);
Boss Boss1 = new Boss("John Smith");
Boss Boss2 = new Boss("Cynthia Jameson");
Employee Employee1 = new Employee("David Ryle");
Employee Employee2 = new Employee("Samantha Sun");
Employee Employee3 = new Employee("Dick Banshee");
// Method 1
eventsSubscriptions["1"].Subscribe(Boss1);
eventsSubscriptions["1"].Subscribe(Employee1);
//// Method 2
eventsSubscriptions["2"].Subscribe(Boss2);
eventsSubscriptions["2"].Subscribe(Employee2);
//// Method 3
eventsSubscriptions["3"].Subscribe(Employee3);
}
public void RaiseAllEvents()
{
eventsSubscriptions.RaiseAllEvents();
}
}
}
Class EventsSubscriptions
namespace MyExample
{
public class EventsSubscriptions
{
private Dictionary<string, Subscription> subscriptions = new Dictionary<string, Subscription>();
public Subscription this[string id]
{
get
{
Subscription subscription = null;
subscriptions.TryGetValue(id, out subscription);
if (subscription == null)
{
subscription = new Subscription();
subscriptions.Add(id, subscription);
}
return subscription;
}
}
public void RaiseAllEvents()
{
foreach (Subscription subscription in subscriptions.Values)
{
Subscription iterator = subscription;
while (iterator != null)
{
iterator.RaiseEvent();
iterator = iterator.NextSubscription;
}
}
}
}
}
Class Subscription
namespace MyExample
{
public class Subscription
{
private object suscribedObject;
private EventHandler eventHandler;
private Subscription nextSubscription;
public object SuscribedObject
{
set
{
suscribedObject = value;
}
}
public EventHandler EventHandler
{
set
{
eventHandler = value;
}
}
public Subscription NextSubscription
{
get
{
return nextSubscription;
}
set
{
nextSubscription = value;
}
}
public void Subscribe(object obj)
{
if (suscribedObject == null)
{
suscribedObject = obj;
}
else
{
if (nextSubscription != null)
{
nextSubscription.Subscribe(obj);
}
else
{
Subscription newSubscription = new Subscription();
newSubscription.eventHandler = this.eventHandler;
nextSubscription = newSubscription;
newSubscription.Subscribe(obj);
}
}
}
public void RaiseEvent()
{
if (eventHandler != null)
{
eventHandler(suscribedObject, new System.EventArgs());
}
}
}
}