Skip to main content

Disposer by Hakan Fıstık

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Disposer

A source generator for creating best-practice Disposing boilerplate using a [Disposable] attribute

Author: Hakan Fıstık

NuGet: https://www.nuget.org/packages/Disposer/

You can find more details at https://github.com/HakamFostok/Disposer

Source : https://github.com/HakamFostok/Disposer

Original Readme

note

Disposer

A Source Generator package that generates extension methods for enums, to allow fast "reflection".

This source generator requires the .NET 6 SDK. You can target earlier frameworks like .NET Core 3.1 etc, but the SDK must be at least 6.0.100

Add the package to your application using

dotnet add package Disposer

This adds a <PackageReference> to your project. You can additionally mark the package as PrivateAsets="all".

Setting PrivateAssets="all" means any projects referencing this one won't get a reference to the Disposer package.

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<PackageReference Include="Disposer" Version="1.0.0"
PrivateAssets="all" />

</Project>

Adding the package will automatically add a marker attribute, [Disposable], to your project.

To use the generator, add the [EnumExtensions] attribute to an enum. For example:

[Disposer.Disposable]
public class MyClass
{
partial void DisposeManaged()
{
// free managed resources here
}

partial void DisposeUnmanaged()
{
// free Unmanaged resources here
}
}

This will generate a class another partial class which implement IDisposable interface:

partial class MyClass : global::System.IDisposable
{
partial void DisposeManaged();
partial void DisposeUnmanaged();

private bool disposed = false;

~MyClass()
{
Dispose(false);
}

private void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
DisposeManaged();
}

DisposeUnmanaged();

disposed = true;
}

public void Dispose()
{
Dispose(true);
global::System.GC.SuppressFinalize(this);
}
}

About

note

Generates partials for dispose resources

How to use

Example ( source csproj, source files )

This is the CSharp Project that references Disposer

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Disposer" Version="1.0.4" PrivateAssets="all" />

</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Disposer source generator
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace IDisposableGeneratorDemo
{
partial class DALDB : global::System.IDisposable
{
partial void DisposeManaged();
partial void DisposeUnmanaged();

private bool disposed = false;

~DALDB()
{
Dispose(false);
}

private void Dispose(bool disposing)
{
if (disposed)
return;

if (disposing)
{
DisposeManaged();
}

DisposeUnmanaged();

disposed = true;
}

public void Dispose()
{
Dispose(true);
global::System.GC.SuppressFinalize(this);
}
}
}

Usefull

Download Example (.NET C# )

Share Disposer

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Disposer

In the same category (Disposer) - 3 other generators

BenutomoAutomaticDisposeImplSourceGenerator

DisposableHelpers

IDisposableGenerator