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

RefCountDisposable

public sealed class RefCountDisposable : ICancelable, IDisposable
Represents a disposable resource that only disposes its underlying disposable resource when all GetDisposabledependent disposable objects have been disposed.
public bool IsDisposed { get; }

Gets a value that indicates whether the object is disposed.

public RefCountDisposable(IDisposable disposable)

Initializes a new instance of the RefCountDisposable class with the specified disposable.

public RefCountDisposable(IDisposable disposable, bool throwWhenDisposed)

Initializes a new instance of the RefCountDisposable class with the specified disposable.

public void Dispose()

Disposes the underlying disposable only when all dependent disposables have been disposed.

Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.