What you describe looks like the Mediator pattern, in which objects communicate through messages. These messages can be implemented as events, callbacks, or any other mechanism.
You could use an implementation like MVVM Light's Messenger
class (this framework is intended for use with WPF and Silverlight, but you can get the code for this particular class and use it in WinForms)
// Register for a specific message type
Messenger.Default.Register<TypeOfTheMessage>(this, DoSomething);
...
// Called when someone sends a message of type TypeOfTheMessage
private void DoSomething(TypeOfTheMessage message)
{
// ...
}
// Send a message to all objects registered for this type of message
Messenger.Default.Send(new TypeOfTheMessage(...));
A big advantage of the Messenger
class over a static event is that it uses weak references, so it doesn't prevent garbage collection of subscribed objects, which reduces the risk of memory leaks.
See this link for details about the Messenger
class
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…