Example implementation of weak events using .NET's WeakEventManager
Asked Answered
K

2

25

Is there an example implementation of weak events using .NET's WeakEventManager?

I'm trying to implement it by following the "Notes to Inheritors" in the documentation, but it is vague. For example, I can't figure out how to call ProtectedAddListener from my static AddListener function in my custom manager.

Kidnap answered 19/8, 2010 at 17:43 Comment(0)
K
28

I figured it out on my own following the guidelines in the "Notes for Inheritors" section of the WeakEventManager documentation. Here's a basic implementation of WeakEventManager. The class sourcing the event is named PropertyValue and the event is named Changed.

public class PropertyValueChangedEventManager : WeakEventManager
{
    public static PropertyValueChangedEventManager CurrentManager
    {
        get
        {
            var manager_type = typeof(PropertyValueChangedEventManager);
            var manager = WeakEventManager.GetCurrentManager(manager_type) as PropertyValueChangedEventManager;

            if (manager == null)
            {
                manager = new PropertyValueChangedEventManager();
                WeakEventManager.SetCurrentManager(manager_type, manager);
            }

            return manager;
        }
    }

    public static void AddListener(PropertyValue source, IWeakEventListener listener)
    {
        CurrentManager.ProtectedAddListener(source, listener);
    }

    public static void RemoveListener(PropertyValue source, IWeakEventListener listener)
    {
        CurrentManager.ProtectedRemoveListener(source, listener);
    }

    protected override void StartListening(object source)
    {
        ((PropertyValue)source).Changed += DeliverEvent;
    }

    protected override void StopListening(object source)
    {
        ((PropertyValue)source).Changed -= DeliverEvent;
    }
}
Kidnap answered 19/8, 2010 at 20:6 Comment(4)
Microsoft provides PropertyChangedEventManager (msdn.microsoft.com/en-us/library/…) and CollectionChangedEventManager (msdn.microsoft.com/en-us/library/…) built-in to .NET.Kidnap
Thanks for your example. Athough the shorter implementation will be ((PropertyValue)source).Changed += DeliverEvent;Significs
@alpha-mouse: Where exactly is the "weak" in your "shorter implementation"? I cannot find it.Pitchdark
@Paul Groke: my comment was relevant to the previous unedited version of this answer. Now I have no objections at all.Significs
H
7

Sharp Observation is an open source project that has an easy to use implementation. You might want to take a look at their code or just use it as-is.

Usage

The MakeWeak() method returns a new delegate which invokes the same target as the original delegate, but holds a weak reference to the target so that the listener is not kept alive by the delegate:

var handler= new PropertyChangingEventHandler(listener.HandleChange);
observable.PropertyChanging += handler.MakeWeak<PropertyChangingEventHandler>();

Limitations

The current implementation has the following restrictions on delegates:

  • Return values are not supported.
  • Arguments of Out and Ref are not supported.
Harbin answered 3/11, 2011 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.