/// <summary> /// Attaches the specified source. /// </summary> /// <param name="source">The source.</param> /// <param name="subscription">The subscription.</param> /// <param name="unsubscription">The unsubscription.</param> public void Attach(object source, Action <EventHandler> subscription, Action <EventHandler> unsubscription) { if (_weakEventListenersDict.ContainsKey(source)) // not design for (note we could tweak the code to manage a list of WeakEventListener by source if needed) { _weakEventListenersDict[source].Detach(); Debug.Assert(false, "Listening twice a source"); } var target = (TInstance)_weakInstance.Target; if (null != target) { var weakEventListener = new WeakEventListener <TInstance, object, EventArgs>(target) { OnEventAction = OnEventAction, OnDetachAction = wel => unsubscription(wel.OnEvent) }; subscription(weakEventListener.OnEvent); _weakEventListenersDict[source] = weakEventListener; } else { Debug.Assert(false, "A disposed instance tries to listen"); } }
public void Detach(object source) { WeakEventListener <TInstance, object, EventArgs> weakEventListener = _weakEventListenersDict[source]; if (weakEventListener != null) { weakEventListener.Detach(); _weakEventListenersDict.Remove(source); } else { Debug.Assert(false, "Trying to detach inexisting WeakEventListener"); } }