Coplt.Dropping by 2A5F
NuGet / site data
Details
Info
info
Name: Coplt.Dropping
Auto gen dispose pattern
Author: 2A5F
NuGet: https://www.nuget.org/packages/Coplt.Dropping/
You can find more details at https://github.com/2A5F/Coplt.Dropping
Original Readme
note
Coplt.Dropping
Auto gen dispose pattern
- Auto handle
Dispose(bool dispoing)
pattern - Auto handle destructor/finalizer
- Allow multiple drops
- Specify the drop order
[Drop(Order = X)]
- The first argument of
Drop
target method can bebool disposing
- Mark Drop directly on fields and properties (requires target type have
Dispose
method) - Dose not supported
AsyncDispose
, too complicated, it is recommended to implement it manually Drop
can mark on static methods, will passthis
on first argument, if havebool disposing
will be the second argument
Example
-
Basic usage
[Dropping]
public partial class Foo1
{
[Drop]
public void Drop()
{
Console.WriteLine(1);
}
}Generate output:
Foo1.dropping.g.cs
About
note
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 Coplt.Dropping
<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>
This is the use of Coplt.Dropping 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 Coplt.Dropping in DALDB.cs
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();
}
}
This is the use of Coplt.Dropping in ConnectionDB.cs
namespace IDisposableGeneratorDemo;
class ConnectionDB : IDisposable
{
public void Dispose()
{
Console.WriteLine("disposing connectiondb");
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- IDisposableGeneratorDemo.DALDB.dropping.g.cs
// <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
Useful
Download Example (.NET C#)
Share Coplt.Dropping
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Coplt.Dropping
aaa
Category "Disposer" has the following generators:
1 BenutomoAutomaticDisposeImplSourceGenerator
4 Disposer