Table of Contents

Class DisposalTracker

Namespace
Xunit.Sdk

Tracks disposable objects, and disposes them in the reverse order they were added to the tracker. Supports both IDisposable and IAsyncDisposable. You can either directly dispose this object (via DisposeAsync()), or you can enumerate the items contained inside of it (via TrackedObjects). Also supports hand-registering disposal actions via AddAction(Action) and AddAsyncAction(Func<ValueTask>). Note that an object implements both interfaces, this will only call DisposeAsync() and will not call Dispose().

public class DisposalTracker : IAsyncDisposable
Inheritance
DisposalTracker
Implements
Inherited Members
Extension Methods

Properties

TrackedObjects

Gets a list of the items that are currently being tracked.

public IReadOnlyCollection<object> TrackedObjects { get; }

Property Value

IReadOnlyCollection<object>

Methods

Add(object?)

Add an object to be disposed. It may optionally support IDisposable and/or IAsyncDisposable.

public void Add(object? @object)

Parameters

object object

The object to be disposed.

AddAction(Action)

Add an action to the list of things to be done during disposal.

public void AddAction(Action cleanupAction)

Parameters

cleanupAction Action

The cleanup action.

AddAsyncAction(Func<ValueTask>)

Add an action to the list of things to be done during disposal.

public void AddAsyncAction(Func<ValueTask> cleanupAction)

Parameters

cleanupAction Func<ValueTask>

The cleanup action.

AddRange(IEnumerable<object?>)

Add a collection of objects to be disposed. They may optionally support IDisposable and/or IAsyncDisposable.

public void AddRange(IEnumerable<object?> collection)

Parameters

collection IEnumerable<object>

The objects to be disposed.

Clear()

Removes all objects from the disposal tracker.

public void Clear()

DisposeAsync()

Disposes all the objects that were added to the disposal tracker, in the reverse order of which they were added. For any object which implements both IDisposable and IAsyncDisposable we will favor DisposeAsync() and not call Dispose().

public ValueTask DisposeAsync()

Returns

ValueTask