/// <summary> /// Add a new Action to be run at time of disposal /// </summary> /// <param name="dispose">Action to run at disposal</param> /// <returns>A disposable object that, when disposed, will run the given action</returns> public IDisposable Add(Action dispose) { if (IsDisposed) { throw new ObjectDisposedException("CompositeDisposable"); } IDisposable ret = null; lock (disposablesLock) { ret = new AnonymousDisposable(dispose); Disposables.Add(ret); } return(ret); }
/// <summary> /// Add a special Action set, including action to run at addition and disposal, with a given handler object /// </summary> /// <typeparam name="TDelegate">Type of handler object to pass to the add and remove actions</typeparam> /// <param name="add">Action to be run on addition of the new disposable</param> /// <param name="remove">Action to be run on disposal of the new disposable</param> /// <param name="handler">Object to be passed to the add and remove actions</param> /// <returns>New Disposable object that will run the remove Action on disposal</returns> public IDisposable Add <TDelegate>(Action <TDelegate> add, Action <TDelegate> remove, TDelegate handler) { if (IsDisposed) { throw new ObjectDisposedException("CompositeDisposable"); } IDisposable ret = null; lock (disposablesLock) { if (add != null) { add(handler); } ret = new AnonymousDisposable(() => remove(handler)); Disposables.Add(ret); } return(ret); }