public static void Unsubscribe <TOpenEventHandler, TStrongHandler>( DelegateCollectionBase <TOpenEventHandler, TStrongHandler> handlers, Delegate handler) where TOpenEventHandler : Delegate where TStrongHandler : struct { if (handler is null) { throw new ArgumentNullException(nameof(handler)); } if (handlers is null) { return; } var singleHandlers = handler .GetInvocationList(); lock (handlers) { foreach (var singleHandler in singleHandlers) { handlers.Remove(singleHandler); } handlers.CollectDeleted(); } }
public static IEnumerable <TStrongHandler> GetValidHandlers <TOpenEventHandler, TStrongHandler>( DelegateCollectionBase <TOpenEventHandler, TStrongHandler> handlers) where TOpenEventHandler : Delegate where TStrongHandler : struct { if (handlers is null) { return(Enumerable.Empty <TStrongHandler>()); } List <TStrongHandler> validHandlers; lock (handlers) { validHandlers = new List <TStrongHandler>(handlers.Count); for (int i = 0; i < handlers.Count; i++) { var weakHandler = handlers[i]; if (weakHandler != null) { if (weakHandler.TryGetStrongHandler() is TStrongHandler handler) { validHandlers.Add(handler); } else { handlers.Invalidate(i); } } } handlers.CollectDeleted(); } return(validHandlers); }