取消订阅泛型 class 的事件,其类型参数在泛型方法中指定

Unsubscribe from an event of a generic class whose type parameter is specified within a generic method

如何取消订阅泛型 class 的事件,其类型参数在泛型方法中指定如下?

public class ListLayoutControl : Control
{
    NotifyCollectionChangedEventHandler handler = null;

    public void AttachTo<T, U>(T list) where T : INotifyCollectionChanged, ICollection<U>
    {
        handler = delegate (object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateLayout(list.Count);
        };
        list.CollectionChanged += handler;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            // unsubscribe ??
        }
        base.Dispose(disposing);
    }
}

在单独的委托中捕获退订并在处理时执行它:

private Action _unsubscribeHandler;
public void AttachTo<T, U>(T list) where T : INotifyCollectionChanged, ICollection<U>
{
    NotifyCollectionChangedEventHandler handler = delegate (object sender, NotifyCollectionChangedEventArgs e)
    {
        UpdateLayout(list.Count);
    };
    list.CollectionChanged += handler;
    _unsubscribeHandler = () => {
        list.CollectionChanged -= handler;     
    };
}

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        _unsubscribeHandler?.Invoke();
    }
    base.Dispose(disposing);
}

如果可以使用不同的列表多次调用 AttachTo - 在 List<Action> 中收集取消订阅处理程序并在处理时全部执行它们。