Esempio n. 1
0
 /// <summary>
 /// This method is provided to find a <see cref="WeakEventDelegate{TEventArgs}"/>
 /// that was added here by passing this <paramref name="weakReference"/>
 /// to <see cref="AddHandler"/> --- specifying to use this object as the
 /// weak reference to retain this <see cref="EventHandler{TEventArgs}"/>. If
 /// an explicit object was passed to the method as the object used to
 /// retain the weak reference here, then this returns true if this is the object.
 /// Note that if <see langword="null"/> was passed as the weak reference, then
 /// this has used the <see cref="EventHandler{TEventArgs}"/>
 /// <see cref="Delegate.Target"/> as the weak reference --- and so if that
 /// Target is passed to this method, this will return true. Note also
 /// that if the weak reference is no longer alive then this returns false.
 /// </summary>
 /// <param name="weakReference">Not null.</param>
 /// <param name="weakEventDelegate">The result if found.</param>
 /// <returns>True if this object WAS the object, or
 /// <see cref="Delegate.Target"/> passed to <see cref="AddHandler"/> to hold
 /// the weak reference here --- and this reference IS still alive.</returns>
 public bool TryGetKeyedDelegate(object weakReference, out WeakEventDelegate <TEventArgs> weakEventDelegate)
 {
     if (weakReference == null)
     {
         throw new ArgumentNullException(nameof(weakReference));
     }
     foreach (WeakEventDelegate <TEventArgs> liveDelegate in enumerateLiveDelegates())
     {
         if (liveDelegate.IsKeyedByWeakReference(weakReference))
         {
             weakEventDelegate = liveDelegate;
             return(true);
         }
     }
     weakEventDelegate = null;
     return(false);
 }
Esempio n. 2
0
 /// <summary>
 /// Adds an event handler. If the <paramref name="weakReference"/>
 /// is given, it is used as a weak reference that
 /// will release both it and the <paramref name="eventHandler"/>
 /// when it becomes released. Otherwise this will test
 /// the Target object of the <paramref name="eventHandler"/>,
 /// and if not null, uses that. Note that if
 /// both are null then the weak reference used IS the <c>eventHandler</c> --- and the
 /// code that constructed that must hold a strong reference (the Target is null if the delegate
 /// is a static method; and a lambda as an event handler delegate may be immediately
 /// collected).
 /// </summary>
 /// <param name="eventHandler">NOT null. Will be weakly held here.</param>
 /// <param name="weakReference">Optional object will determine the weak retention of the
 /// <c>eventHandler</c>.</param>
 /// <returns>The actual <see cref="WeakEventDelegate{TEventArgs}"/> that has been created
 /// and added here.</returns>
 /// <exception cref="ArgumentNullException"></exception>
 public WeakEventDelegate <TEventArgs> AddHandler(
     EventHandler <TEventArgs> eventHandler,
     object weakReference = null)
 {
     if (eventHandler == null)
     {
         throw new ArgumentNullException(nameof(eventHandler));
     }
     Purge();
     lock (EventDelegates) {
         WeakEventDelegate <TEventArgs> weakEventDelegate
             = new WeakEventDelegate <TEventArgs>(eventHandler, weakReference);
         EventDelegates.Add(weakEventDelegate);
         weakEventDelegate.Disposed += handleWeakEventDelegateDisposed;
         return(weakEventDelegate);
     }
 }