All projects

Explicit lifetime source generation

DisposableGenerator

Keep ownership visible in handwritten code and generate the repetitive lifetime machinery.

StatusActive
TierMaintained
DomainTools
Repositorywieslawsoltes/DisposableGenerator

An incremental C# source generator for explicit IDisposable and IAsyncDisposable ownership, inheritance-safe cleanup, dynamic registration, diagnostics, and optional unmanaged finalization.

DisposableGenerator separates the lifetime decision from its mechanical implementation. The developer marks what is owned, what is borrowed, and whether cleanup is synchronous or asynchronous; generated code then makes ordering, concurrency, inheritance, and failure behavior consistent.

01

Explicit owned and borrowed members

02

Sync, async, and inheritance-safe disposal

03

Dynamic lifetime registration

04

Diagnostics, hooks, and configurable ordering

Where it fits.

  • .NET libraries with non-trivial resource ownership
  • MVVM applications managing subscriptions and child view models
  • Native and interop code that needs deterministic cleanup contracts
.NET

8.0–10.0 consumers

IDisposable

Generated

IAsyncDisposable

Optional

NativeAOT

Compile-time only

Real output,
not a mockup.

DisposableGenerator GitHub repository preview
The repository includes generator sources, ownership guidance, a complete diagnostic catalog, integration consumers, and Avalonia/ReactiveUI samples.

Clear layers.
Replaceable seams.

01

Declare

Attributes record owned members, borrowed members, and the required sync or async contract.

02

Analyze

Diagnostics reject conflicting lifetimes, unsafe inheritance, and unsupported finalizer shapes.

03

Generate

Thread-safe entry points, hooks, ordering, registration, and base chaining are emitted at compile time.

04

Dispose

Idempotent cleanup follows the selected failure policy and suppresses finalization when appropriate.

26ownership diagnostics
3 OS × 3 TFMspackage integration matrix
Zeroruntime dependencies
LiveGitHub metadata

From package
to first frame.

Choose the package workflow used by your repository, then start from the smallest working example. Replace VERSION with the current stable or prerelease version shown on NuGet.

01 · Install
Terminal
dotnet add package DisposableGenerator
02 · Use · csharp
QuickStart.cs
[GenerateDisposable]
public sealed partial class EditorViewModel
{
[DisposeMember]
private readonly IDisposable _subscription;
[BorrowedMember]
private readonly IDisposable _service;
}

Start focused.
Compose as needed.

The repository is the source of truth.

Use the current documentation and samples, inspect changes, report issues, or support continued open-source maintenance.

ProMarkdown

Search everything.

Loading the index…

move open esc close

Preparing metadata and full-text search…