If object A listens to an event from object B, object B will keep object A alive. Is there a standard implementation of weak events that would prevent this? I know WPF has s
What advantages does Dustin's implementation have compared to the WPF's WeakEventManager class which simply wraps the target object as well as the delegate into a weak reference:
public Listener(object target, Delegate handler)
{
this._target = new WeakReference(target);
this._handler = new WeakReference((object) handler);
}
In my opinion this approach is more flexible, since it does not require the implementation to pass the target instance as a parameter during the invocation of the event handler:
public void Invoke(object sender, E e)
{
T target = (T)m_TargetRef.Target;
if (target != null)
m_OpenHandler(target, sender, e);
This also allows the use of anomymous methods instead of an instance method (that seems to be also a disadvantage of the Dustin's implementation).