<PackageReference Include="System.Reactive" Version="4.1.4" />

AnonymousDisposable<TState>

sealed class AnonymousDisposable<TState> : ICancelable, IDisposable
Represents a Action-based disposable that can hold onto some state.
public bool IsDisposed { get; }

Gets a value that indicates whether the object is disposed.

public AnonymousDisposable(TState state, Action<TState> dispose)

Constructs a new disposable with the given action used for disposal.

public void Dispose()

Calls the disposal action if and only if the current instance hasn't been disposed yet.