src/Misc/EFTools/XmlCore/Core/Context/ServiceManager.cs
/// <summary>
///     This is a helper method that performs a Delegate.Remove, but knows
///     how to unwrap delegates that are proxies to generic callbacks.  Use
///     this in your Unsubscribe implementations.
/// </summary>
/// <param name="existing">The existing delegate to remove the callback from.</param>
/// <param name="toRemove">The callback to remove.</param>
/// <returns>A new value to assign to the existing delegate.</returns>
This is a helper method that performs a Delegate.Remove, but knows
how to unwrap delegates that are proxies to generic callbacks.  Use
this in your Unsubscribe implementations.
This is a helper method that performs a Delegate . Remove but knows how to unwrap delegates that are proxies to generic callbacks . Use this in your Unsubscribe implementations .
protected static Delegate RemoveCallback(Delegate existing, Delegate toRemove)
        {
            if (existing == null)
            {
                return null;
            }
            if (toRemove == null)
            {
                return existing;
            }
            var toRemoveProxy = toRemove.Target as ICallbackProxy;
            if (toRemoveProxy == null)
            {
                return Delegate.Remove(existing, toRemove);
            }
            toRemove = toRemoveProxy.OriginalDelegate;
            var invocationList = existing.GetInvocationList();
            var removedItems = false;
            for (var idx = 0; idx < invocationList.Length; idx++)
            {
                var item = invocationList[idx];
                var itemProxy = item.Target as ICallbackProxy;
                if (itemProxy != null)
                {
                    item = itemProxy.OriginalDelegate;
                }
                if (item.Equals(toRemove))
                {
                    invocationList[idx] = null;
                    removedItems = true;
                }
            }
            if (removedItems)
            {
                existing = null;
                foreach (var d in invocationList)
                {
                    if (d != null)
                    {
                        if (existing == null)
                        {
                            existing = d;
                        }
                        else
                        {
                            existing = Delegate.Combine(existing, d);
                        }
                    }
                }
            }
            return existing;
        }
protected static Delegate RemoveCallback ( Delegate existing , Delegate toRemove ) { if ( existing == null ) { return null ; } if ( toRemove == null ) { return existing ; } var toRemoveProxy = toRemove . Target as ICallbackProxy ; if ( toRemoveProxy == null ) { return Delegate . Remove ( existing , toRemove ) ; } toRemove = toRemoveProxy . OriginalDelegate ; var invocationList = existing . GetInvocationList ( ) ; var removedItems = false ; for ( var idx = 0 ; idx < invocationList . Length ; idx ++ ) { var item = invocationList [ idx ] ; var itemProxy = item . Target as ICallbackProxy ; if ( itemProxy != null ) { item = itemProxy . OriginalDelegate ; } if ( item . Equals ( toRemove ) ) { invocationList [ idx ] = null ; removedItems = true ; } } if ( removedItems ) { existing = null ; foreach ( var d in invocationList ) { if ( d != null ) { if ( existing == null ) { existing = d ; } else { existing = Delegate . Combine ( existing , d ) ; } } } } return existing ; }
This is a helper method that performs a Delegate.Remove, but knows
how to unwrap delegates that are proxies to generic callbacks.
This is a helper method that performs a Delegate . Remove but knows how to unwrap delegates that are proxies to generic callbacks .
// The item to be removed is a normal delegate.  Just call // Delegate.Remove // We must create a new delegate containing the  // invocation list that is is left
A new value to assign to the existing delegate.
A new value to assign to the existing delegate .
existing
Delegate
The existing delegate to remove the callback from.
The existing delegate to remove the callback from .
toRemove
Delegate
The callback to remove.
The callback to remove .