Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

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`