IDisposableGenerator by Els_kom Official Organization
Nuget / site data
Details
Info
Name: IDisposableGenerator
Source Generator Generating the Dispose functions in Disposables.
Author: Els_kom Official Organization
NuGet: https://www.nuget.org/packages/IDisposableGenerator/
You can find more details at https://github.com/Elskom/IDisposableGenerator
Original Readme
IDisposableGenerator
Source Generator Generating the Dispose functions in Disposables.
Code Ownership
All code used is copyright of Elskom org, with the exception of Roslyn which is copyright of the .NET Foundation and it's contributors.
The dependencies of the unit tests are copyright of their respective owners.
Status
This project is currently actively maintained whenever an issue happens (or whenever major roslyn changes happens that break it).
Purpose
This project is for easily generating the dispose functions of disposable types using attributes to control the generator on how it writes the generated code. This results in code that is more maintainable and cleaner than if you had to implement the IDisposable interface yourself. Disposable types require marking the type as partial to properly compile the generated code.
Documentation
It is currently in the works.
Badges
Package | Version |
---|---|
IDisposableGenerator |
About
Generating disposable
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- DALDB.cs
- ConnectionDB.cs
This is the CSharp Project that references IDisposableGenerator
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IDisposableGenerator" Version="1.1.1" OutputItemType="Analyzer" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
This is the use of IDisposableGenerator 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 IDisposableGenerator in DALDB.cs
namespace IDisposableGeneratorDemo;
[IDisposableGenerator.GenerateDispose(false)]
partial class DALDB :IDisposable
{
[IDisposableGenerator.DisposeField(true)]
private ConnectionDB cn;
[IDisposableGenerator.DisposeField(true)]
private ConnectionDB cn1;
public DALDB()
{
cn = new ConnectionDB();
cn1=new ConnectionDB();
}
}
This is the use of IDisposableGenerator in ConnectionDB.cs
namespace IDisposableGeneratorDemo;
class ConnectionDB : IDisposable
{
public void Dispose()
{
Console.WriteLine("disposing connectiondb");
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Disposables.g.cs
- GeneratedAttributes.g.cs
// <autogenerated/>
namespace IDisposableGeneratorDemo;
internal partial class DALDB : IDisposable
{
private bool isDisposed;
internal bool IsOwned { get; set; }
/// <summary>
/// Cleans up the resources used by <see cref="DALDB"/>.
/// </summary>
public void Dispose() => this.Dispose(true);
private void Dispose(bool disposing)
{
if (!this.isDisposed && disposing)
{
if (this.IsOwned)
{
this.cn?.Dispose();
this.cn = null;
this.cn1?.Dispose();
this.cn1 = null;
}
this.isDisposed = true;
}
}
}
// <autogenerated/>
#pragma warning disable SA1636, 8618
namespace IDisposableGenerator
{
using System;
// used only by a source generator to generate Dispose() and Dispose(bool).
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal class CallOnDisposeAttribute : Attribute
{
public CallOnDisposeAttribute()
{
}
}
// used only by a source generator to generate Dispose() and Dispose(bool).
[AttributeUsage(AttributeTargets.Event | AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
internal class DisposeFieldAttribute : Attribute
{
public DisposeFieldAttribute(bool owner)
{
}
}
// used only by a source generator to generate Dispose() and Dispose(bool).
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal class GenerateDisposeAttribute : Attribute
{
public GenerateDisposeAttribute(bool stream)
{
}
}
// used only by a source generator to generate Dispose() and Dispose(bool).
[AttributeUsage(AttributeTargets.Event | AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
internal class NullOnDisposeAttribute : Attribute
{
public NullOnDisposeAttribute()
{
}
}
}
#pragma warning restore SA1636, 8618
Usefull
Download Example (.NET C# )
Share IDisposableGenerator
https://ignatandrei.github.io/RSCG_Examples/v2/docs/IDisposableGenerator