Is it possible to stop multiple subscribers from subscribing to an event?
I have created a quick example snippet to give my question some context but unfortunately I can’t test it right now because I’m not at my VS machine.
The goal is to:
- Return an empty list if there are no subscribers.
- Return the return value of a single subscriber.
- Throw an exception if more than one subscriber tries to subscribe to the event (this is the crux of the matter).
Is this possible?
public delegate List<IBaseWindow> GetWindowListDelegate();
public static event GetWindowListDelegate GetWindowListEvent;
public List<IBaseWindow> GetWindowList() {
if (GetWindowListEvent == null) {
return new List<IBaseWindow>();
}
return GetWindowListEvent();
}
Note: I’m using .NET 3.5 sp1.
It sounds like you don’t need an event – just expose the delegate itself and allow callers to set the delegate reference on their own.