public WeakDelegateSuragate Clone() { WeakDelegateSuragate suragate = new WeakDelegateSuragate(delegateType, delegates.Length); delegates.CopyTo(suragate.delegates, 0); return(suragate); }
/// <summary> /// Concatenates the invocation lists of two delegates. /// </summary> /// <typeparam name="TDelegate">The type of delegate to combine.</typeparam> /// <param name="first">The delegate whose invocation list comes first.</param> /// <param name="last">The delegate whose invocation list comes last.</param> /// <returns>A new delegate with a weak connection to both <paramref name="first"/> and <paramref name="last"/>. If one is <see langword="null"/> but the other is not, returns a delegate with a weak connection to just that the parameter that is not <see langword="null"/>. If both are <see langword="null"/>, returns <see langword="null"/>.</returns> /// <seealso cref="Delegate.Combine(Delegate, Delegate)"/> public static TDelegate Combine <[DelegateConstraint] TDelegate>(TDelegate first, TDelegate last) where TDelegate : class { if (ReferenceEquals(first, null) && ReferenceEquals(last, null)) { return(null); } Delegate firstDelegate = first as Delegate; Delegate lastDelegate = last as Delegate; Delegate finalDelegate = CreateDynamicDelegate(first, last, firstDelegate, lastDelegate); WeakDelegateSuragate firstSuragate = null; if (firstDelegate != null) { GetCombinedHolder(firstDelegate.Method, out firstSuragate); } WeakDelegateSuragate lastSuragate = null; if (lastDelegate != null) { GetCombinedHolder(lastDelegate.Method, out lastSuragate); } WeakDelegateSuragate finalSuragate = firstSuragate != null ? lastSuragate != null?firstSuragate.Add(lastSuragate) : lastDelegate != null?firstSuragate.Add(firstDelegate) : firstSuragate.Clone() : lastSuragate != null ? firstDelegate != null?lastSuragate.Add(firstDelegate) : lastSuragate.Clone() : firstDelegate != null ? lastDelegate != null ? new WeakDelegateSuragate(firstDelegate, lastDelegate) : new WeakDelegateSuragate(firstDelegate) : new WeakDelegateSuragate(lastDelegate); suragates.Add(finalDelegate.Method, new WeakReference <WeakDelegateSuragate>(finalSuragate)); if (firstDelegate != null && firstDelegate.Target != null) { weakReference.Add(firstDelegate.Target, finalSuragate); } if (lastDelegate != null && lastDelegate.Target != null) { weakReference.Add(lastDelegate.Target, finalSuragate); } return(finalDelegate as TDelegate); }
public WeakDelegateSuragate Add(WeakDelegateSuragate other) { WeakDelegateSuragate suragate = new WeakDelegateSuragate(delegateType, delegates.Length + other.delegates.Length); delegates.CopyTo(suragate.delegates, 0); other.delegates.CopyTo(suragate.delegates, delegates.Length); return(suragate); }
public WeakDelegateSuragate Add(Delegate @delegate) { WeakDelegateSuragate suragate = new WeakDelegateSuragate(delegateType, delegates.Length + 1); delegates.CopyTo(suragate.delegates, 0); delegates[delegates.Length - 1] = new DelegateBreakout(@delegate, Clean); return(suragate); }
private static bool GetCombinedHolder(MethodBase method, out WeakDelegateSuragate holder) { WeakReference <WeakDelegateSuragate> wr; if (suragates.TryGetValue(method, out wr)) { return(wr.TryGetTarget(out holder)); } holder = null; return(false); }
public WeakDelegateSuragate Remove(WeakDelegateSuragate other) { DelegateBreakout[] toRemove = other.delegates; List <DelegateBreakout> breakouts = new List <DelegateBreakout>(delegates.Length); DelegateBreakout[] currentDelegates = delegates; for (int i = 0; i < currentDelegates.Length; i++) { DelegateBreakout breakout = currentDelegates[i]; if (Array.IndexOf(toRemove, breakout) == -1 && breakout.Alive) { breakouts.Add(breakout); } } WeakDelegateSuragate suragate = new WeakDelegateSuragate(delegateType, breakouts.Count); breakouts.CopyTo(suragate.delegates); return(suragate); }
public WeakDelegateSuragate Remove(Delegate @delegate) { using (DelegateBreakout toRemove = new DelegateBreakout(@delegate, null)) { List <DelegateBreakout> breakouts = new List <DelegateBreakout>(delegates.Length); DelegateBreakout[] currentDelegates = delegates; for (int i = 0; i < currentDelegates.Length; i++) { DelegateBreakout breakout = currentDelegates[i]; if (breakout != toRemove && breakout.Alive) { breakouts.Add(breakout); } } WeakDelegateSuragate suragate = new WeakDelegateSuragate(delegateType, breakouts.Count); breakouts.CopyTo(suragate.delegates); return(suragate); } }