Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/2A5F/Coplt.Dropping
Auto gen dispose pattern
https://github.com/2A5F/Coplt.Dropping
Last synced: 26 days ago
JSON representation
Auto gen dispose pattern
- Host: GitHub
- URL: https://github.com/2A5F/Coplt.Dropping
- Owner: 2A5F
- License: mit
- Created: 2024-08-18T07:48:58.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-18T15:22:45.000Z (5 months ago)
- Last Synced: 2024-12-15T23:46:28.610Z (about 1 month ago)
- Language: C#
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- RSCG_Examples - https://github.com/2A5F/Coplt.Dropping
README
# Coplt.Dropping
[![Nuget](https://img.shields.io/nuget/v/Coplt.Dropping)](https://www.nuget.org/packages/Coplt.Dropping/)
![MIT](https://img.shields.io/github/license/2A5F/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 be `bool 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 pass `this` on first argument, if have `bool disposing` will be the second argument## Example
- Basic usage
```cs
[Dropping]
public partial class Foo1
{
[Drop]
public void Drop()
{
Console.WriteLine(1);
}
}
```
Generate output:
Foo1.dropping.g.cs
```cs
//
#nullable enable
using Coplt.Dropping;
public partial class Foo1 : global::System.IDisposable
{
protected virtual void Dispose(bool disposing)
{
if (disposing) Drop();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Foo1()
{
Dispose(false);
}
}
```
- Unmanaged resources
```csharp
[Dropping(Unmanaged = true /* set drop in the class all default is unmanaged */)]
public partial class Foo2
{
[Drop]
private void Drop()
{
Console.WriteLine(1);
}
[Drop(Unmanaged = false)]
private void Drop2()
{
Console.WriteLine(2);
}
}
```
Generate output:
Foo2.dropping.g.cs```cs
//#nullable enable
using Coplt.Dropping;
public partial class Foo2 : global::System.IDisposable
{protected virtual void Dispose(bool disposing)
{
Drop();
if (disposing) Drop2();
}public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}~Foo2()
{
Dispose(false);
}}
```
- Inherit
```cs
[Dropping]
public partial class Foo3 : Foo2
{
[Drop]
private void Drop()
{
Console.WriteLine(1);
}[Drop(Unmanaged = true)]
private void Drop2()
{
Console.WriteLine(2);
}
}
```Generate output:
Foo3.dropping.g.cs```cs
//#nullable enable
using Coplt.Dropping;
public partial class Foo3 : global::System.IDisposable
{protected override void Dispose(bool disposing)
{
if (disposing) Drop();
Drop2();
base.Dispose(disposing);
}}
```
- Sealed And Struct
```cs
[Dropping]
public sealed partial class Foo4
{
[Drop]
public void Drop()
{
Console.WriteLine(1);
}
}[Dropping(Unmanaged = true)]
public sealed partial class Foo5
{
[Drop]
public void Drop()
{
Console.WriteLine(1);
}
}[Dropping]
public partial struct Foo6
{
[Drop]
private void Drop()
{
Console.WriteLine(1);
}
}
```Generate output:
Foo4.dropping.g.cs```cs
//#nullable enable
using Coplt.Dropping;
public partial class Foo4 : global::System.IDisposable
{public void Dispose()
{
Drop();
}}
```
Foo5.dropping.g.cs```cs
//#nullable enable
using Coplt.Dropping;
public partial class Foo5 : global::System.IDisposable
{public void Dispose()
{
Drop();
GC.SuppressFinalize(this);
}~Foo5()
{
Dispose();
}}
```
Foo6.dropping.g.cs```cs
//#nullable enable
using Coplt.Dropping;
public partial struct Foo6 : global::System.IDisposable
{public void Dispose()
{
Drop();
}}
```
More see `Tests`