Skip to content

Implementing Dispose pattern automatically using Source Generator feature.

Notifications You must be signed in to change notification settings

hamedsabzian/Generators.Dispose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to use?

You can install its latest version using your IDE Nuget package manager by using dotnet command as follows:

dotnet add package Generators.Dispose
Package Version
Generators.Dispose NuGet Badge

It generates a partial class for all partial classes that implement IDisposable interface.

public partial class SampleClass : IDisposable
{
}

The generated class declares DisposeManaged partial method that will be called to release managed resources.

public partial class SampleClass : IDisposable
{
    partial void DisposeManaged()
    {
        // Call Dispose method of the managed resources
    }
}

If your class works with unmanaged resources, you need to implement DisposeUnmanaged partial method.

public partial class SampleClass : IDisposable
{
    partial void DisposeManaged()
    {
        // Call Dispose method of the managed resources
    }

    partial void DisposeUnmanaged()
    {
        // Release the managed resources
    }
}

If you have a partial class implements IDisposable interface, but you want to exclude it from generation, just use IgnoreGeneration attribute.

[IgnoreGeneration]
public partial class IgnoredDisposable : IDisposable
{
    // Implement Dispose pattern manually
}

About

Implementing Dispose pattern automatically using Source Generator feature.

Topics

Resources

Stars

Watchers

Forks