https://github.com/HakamFostok/Disposer
Source Generator to generate best-practice IDisposable Implementation
https://github.com/HakamFostok/Disposer
Last synced: 21 days ago
JSON representation
Source Generator to generate best-practice IDisposable Implementation
- Host: GitHub
- URL: https://github.com/HakamFostok/Disposer
- Owner: HakamFostok
- License: mit
- Created: 2022-05-17T09:11:42.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-11T16:31:14.000Z (about 2 years ago)
- Last Synced: 2025-04-08T04:19:03.149Z (30 days ago)
- Language: C#
- Size: 133 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/HakamFostok/Disposer
README
# Disposer
A Source Generator package that generates extension methods for enums, to allow fast "reflection".
> This source generator requires the .NET 6 SDK. You can target earlier frameworks like .NET Core 3.1 etc, but the _SDK_ must be at least 6.0.100
Add the package to your application using
```bash
dotnet add package Disposer
```This adds a `` to your project. You can additionally mark the package as `PrivateAsets="all"`.
> Setting `PrivateAssets="all"` means any projects referencing this one won't get a reference to the _Disposer_ package.
```xml
Exe
net6.0
```
Adding the package will automatically add a marker attribute, `[Disposable]`, to your project.
To use the generator, add the `[EnumExtensions]` attribute to an enum. For example:
```csharp
[Disposer.Disposable]
public class MyClass
{
partial void DisposeManaged()
{
// free managed resources here
}partial void DisposeUnmanaged()
{
// free Unmanaged resources here
}
}
```This will generate a class another partial class which implement `IDisposable` interface:
```csharp
partial class MyClass : global::System.IDisposable
{
partial void DisposeManaged();
partial void DisposeUnmanaged();private bool disposed = false;
~MyClass()
{
Dispose(false);
}private void Dispose(bool disposing)
{
if (disposed)
return;if (disposing)
{
DisposeManaged();
}DisposeUnmanaged();
disposed = true;
}public void Dispose()
{
Dispose(true);
global::System.GC.SuppressFinalize(this);
}
}
```