Explicit lifetime source generation
DisposableGenerator
Keep ownership visible in handwritten code and generate the repetitive lifetime machinery.
Overview
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.
Explicit owned and borrowed members
Sync, async, and inheritance-safe disposal
Dynamic lifetime registration
Diagnostics, hooks, and configurable ordering
Built for
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
Compatibility & status
8.0–10.0 consumers
Generated
Optional
Compile-time only
In the work
Real output,
not a mockup.

Architecture
Clear layers.
Replaceable seams.
Declare
Attributes record owned members, borrowed members, and the required sync or async contract.
Analyze
Diagnostics reject conflicting lifetimes, unsafe inheritance, and unsupported finalizer shapes.
Generate
Thread-safe entry points, hooks, ordering, registration, and base chaining are emitted at compile time.
Dispose
Idempotent cleanup follows the selected failure policy and suppresses finalization when appropriate.
Quick start
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.
dotnet add package DisposableGenerator<PackageReference Include="DisposableGenerator" Version="VERSION" /><PackageVersion Include="DisposableGenerator" Version="VERSION" />[GenerateDisposable]public sealed partial class EditorViewModel{ [DisposeMember] private readonly IDisposable _subscription;
[BorrowedMember] private readonly IDisposable _service;}Package surface
Start focused.
Compose as needed.
Source, docs, samples, and releases
The repository is the source of truth.
Use the current documentation and samples, inspect changes, report issues, or support continued open-source maintenance.