Disposer by Hakan Fıstık
Nuget / site data
Details
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
Original Readme
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
Generates partials for dispose resources
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- ConnectionDB.cs
- DALDB.cs
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>
This is the use of Disposer in Program.cs
using IDisposableGeneratorDemo;
//https://github.com/benutomo-dev/RoslynComponents
using (var db = new DALDB())
{
Console.WriteLine("before releasing");
}
Console.WriteLine("after releasing");
This is the use of Disposer in ConnectionDB.cs
namespace IDisposableGeneratorDemo;
class ConnectionDB : IDisposable
{
public void Dispose()
{
Console.WriteLine("disposing connectiondb");
}
}
This is the use of Disposer in DALDB.cs
namespace IDisposableGeneratorDemo;
[Disposer.Disposable]
partial class DALDB :IDisposable
{
private readonly ConnectionDB cn;
private readonly ConnectionDB cn1;
public DALDB()
{
cn = new ConnectionDB();
cn1=new ConnectionDB();
}
partial void DisposeManaged()
{
cn.Dispose();
cn1.Dispose();
}
partial void DisposeUnmanaged()
{
// free Unmanaged resources here
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- DALDBDisposable.g.cs
//------------------------------------------------------------------------------
// <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