It is certainly possible to achieve what you want to do, but it does not conform to convention -- I would urge you to come up with a different solution that does not involve events.
As explained by Jon Skeet, public events are property-like wrappers around a multicast delegate.
You can think of the standard implementation of an event as a list of functions to be called when something happens.
// From the above link:
// the exposed event
public event EventHandler MyEvent
// multicast delegate field
private EventHandler _myEvent;
// property-like add & remove handlers
public event EventHandler MyEvent
{
add
{
lock (this)
{
_myEvent += value;
}
}
remove
{
lock (this)
{
_myEvent -= value;
}
}
}
... when a developer sees such an event, they expect to be able to subscribe to it without issue as an event basically says "any number of types may register to receive this event notification".
It seems that you are wanting to allow someone to set the implementation that gets the list of windows. What I'd suggest doing is allowing people to manually pass in a delegate, then hold a single delegate instance. Make it explicit that there is only one way to set it; if at all possible I would recommend using constructor injection as this removes all ambiguity -- you can only set the delegate instance once on construction, then it is no longer modifiable by the public API (so class B cannot clobber the delegate that was already set by class A).
E.g.
public class CallsDelegateToDoSomething
{
private Func<List<IBaseWindow>> m_windowLister;
public CallsDelegateToDoSomething(Func<List<IBaseWindow>> windowFunc)
{
m_windowLister = windowFunc;
}
public List<IBaseWindow> GetWindowList()
{
if (windowLister == null)
{
return new List<IBaseWindow>();
}
return m_windowLister();
}
}
If your design doesn't allow for this, then just create SetWindowLister(Func<List<IBaseWindow>> windowLister)
and ClearWindowLister()
methods instead.