| name | ReflectionIT.DisposeGenerator |
| nuget | https://www.nuget.org/packages/ReflectionIT.DisposeGenerator/ |
| link | https://github.com/sonnemaf/ReflectionIT.DisposeGenerator |
| author | Fons Sonnemans |
Automatically implements the IDisposable pattern for classes that contain disposable fields.
This is how you can use ReflectionIT.DisposeGenerator .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ReflectionIT.DisposeGenerator" Version="0.4.2-preview" />
</ItemGroup>
</Project>
The code that you will use is
using IDisposableGeneratorDemo;
// https://github.com/sonnemaf/ReflectionIT.DisposeGenerator
using (var db = new DALDB())
{
Console.WriteLine("before releasing");
}
Console.WriteLine("after releasing");
using ReflectionIT.DisposeGenerator.Attributes;
namespace IDisposableGeneratorDemo;
[Disposable]
partial class DALDB :IDisposable
{
[Dispose]
private ConnectionDB cn;
[Dispose]
private ConnectionDB cn1;
public DALDB()
{
cn = new ConnectionDB();
cn1=new ConnectionDB();
}
}
namespace IDisposableGeneratorDemo;
class ConnectionDB : IDisposable
{
static int count = 0;
public ConnectionDB()
{
Interlocked.Increment(ref count);
}
public void Dispose()
{
Console.WriteLine($"disposing connectiondb {Interlocked.Decrement(ref count)}");
}
}
The code that is generated is
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the ReflectionIT.DisposeGenerator source generator
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#pragma warning disable
#nullable enable annotations
namespace IDisposableGeneratorDemo
{
partial class DALDB
{
/// <summary>
/// Releases all resources used by the current instance.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.DisposeGenerator", "0.4.2.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public void Dispose() {
Dispose(disposing: true);
global::System.GC.SuppressFinalize(this);
}
/// <summary>
/// Tracks whether the current instance has been disposed. This field must not be modified manually.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.DisposeGenerator", "0.4.2.0")]
private bool _isDisposed;
/// <summary>
/// Gets a value indicating whether the current instance has been disposed.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.DisposeGenerator", "0.4.2.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
protected virtual bool IsDisposed => _isDisposed;
/// <summary>
/// Throws an exception if the current instance has been disposed.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.DisposeGenerator", "0.4.2.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
protected void ThrowIfDisposed() {
if (IsDisposed) {
throw new global::System.ObjectDisposedException(nameof(DALDB));
}
}
/// <summary>
/// Releases the unmanaged resources used by the current instance and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">"true" to release managed resources; otherwise, "false".</param>
[global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.DisposeGenerator", "0.4.2.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
protected virtual void Dispose(bool disposing) {
if (_isDisposed) {
return;
}
_isDisposed = true;
if (disposing) {
this.cn?.Dispose();
this.cn1?.Dispose();
}
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/ReflectionIT.DisposeGenerator