RSCG – Coplt.Dropping

RSCG – Coplt.Dropping
 
 

name Coplt.Dropping
nuget https://www.nuget.org/packages/Coplt.Dropping/
link https://github.com/2A5F/Coplt.Dropping
author 2A5F

Generating disposable

 

This is how you can use Coplt.Dropping .

The code that you start with is


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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	 <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
	 <ItemGroup>
	   <PackageReference Include="Coplt.Dropping" Version="0.5.1" OutputItemType="Analyzer" />
	   
	 </ItemGroup>

</Project>


The code that you will use is


using IDisposableGeneratorDemo;
//https://github.com/benutomo-dev/RoslynComponents
using (var db = new DALDB())
{
    Console.WriteLine("before releasing");
}
Console.WriteLine("after releasing");


using Coplt.Dropping;

namespace IDisposableGeneratorDemo;

[Dropping]
partial class DALDB :IDisposable
{
    private ConnectionDB cn;
    private ConnectionDB cn1;

    public DALDB()
    {
        cn = new ConnectionDB();
        cn1=new ConnectionDB();
    }
    [Drop]
    public void Drop()
    {
        cn.Dispose();
        cn1.Dispose();
    }
}



namespace IDisposableGeneratorDemo;

class ConnectionDB : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("disposing connectiondb");
    }
}


 

The code that is generated is

// <auto-generated/>

#nullable enable

using Coplt.Dropping;

namespace IDisposableGeneratorDemo {

internal partial class DALDB : global::System.IDisposable
{

    protected virtual void Dispose(bool disposing)
    {
        if (disposing) Drop();
    }

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

    ~DALDB()
    {
        Dispose(false);
    }

}

} // namespace IDisposableGeneratorDemo

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Coplt.Dropping