FieldDisposerVB.Generator
1.0.0
See the version list below for details.
dotnet add package FieldDisposerVB.Generator --version 1.0.0
NuGet\Install-Package FieldDisposerVB.Generator -Version 1.0.0
<PackageReference Include="FieldDisposerVB.Generator" Version="1.0.0" />
<PackageVersion Include="FieldDisposerVB.Generator" Version="1.0.0" />
<PackageReference Include="FieldDisposerVB.Generator" />
paket add FieldDisposerVB.Generator --version 1.0.0
#r "nuget: FieldDisposerVB.Generator, 1.0.0"
#:package FieldDisposerVB.Generator@1.0.0
#addin nuget:?package=FieldDisposerVB.Generator&version=1.0.0
#tool nuget:?package=FieldDisposerVB.Generator&version=1.0.0
FieldDisposerVB.Generator
A VB.NET source generator that automatically generates field disposal methods for classes that implement IDisposable. This tool simplifies resource management by automatically creating proper dispose patterns for fields marked with the <DisposeField> attribute.
Notes of Usage
If a class/structure contains at least one field marked with <DisposeField>, do NOT implement the IDisposable interface manually. The source generator will automatically implement this interface and handle the disposal of marked fields.
However, when it is a module that contains at least one field marked with <DisposeField>, you must manually call the DisposeModuleFields() method to dispose all the fields marked with this attribute.
Requirements
- Visual Basic .NET (VB.NET) language version 14.0 or higher
- .NET Standard 2.0 or higher
Features
- Automatically generates
IDisposableimplementation - Creates proper dispose pattern with managed/unmanaged resource handling
- Generates finalizer to ensure cleanup
- Handles nullable field disposal safely
- Provides partial method for custom unmanaged resource disposal
- Supports classes, structures, and modules
Installation
Install the package via NuGet:
Install-Package FieldDisposerVB.Generator
Or via .NET CLI:
dotnet add package FieldDisposerVB.Generator
Usage
Basic Usage
Mark fields with the <DisposeField> attribute to have them automatically disposed:
<DisposeField>
Private _stream As FileStream
Public Sub New(filePath As String)
_stream = New FileStream(filePath, FileMode.Open)
End Sub
The source generator will automatically create:
- An
IDisposableinterface implementation - A protected
Dispose(Boolean)method that disposes marked fields - A public
Dispose()method - A finalizer
- A partial method
DisposeUnmanagedResources()for custom cleanup
Complete Example for Classes/Structures
Imports System.IO
' Mark class as partial to allow source generator to add members
Partial Public Class FileManager
' Fields marked with <DisposeField> will be automatically disposed
<DisposeField>
Private _inputStream As FileStream
<DisposeField>
Private _outputStream As MemoryStream
' Regular field that won't be auto-disposed
Private _buffer As Byte()
Public Sub New(inputPath As String)
_inputStream = New FileStream(inputPath, FileMode.Open)
_outputStream = New MemoryStream()
_buffer = New Byte(1023) {}
End Sub
' The source generator will add the entire dispose pattern here
' You can add custom unmanaged resource disposal logic:
Private Sub DisposeUnmanagedResources()
' Add custom disposal logic for unmanaged resources here
' For example: setting large arrays to Nothing
_buffer = Nothing
End Sub
End Class
After compilation, the source generator creates a partial class file that implements the complete dispose pattern:
' Generated file (FileManager_Disposers.g.vb)
Partial Public Class FileManager
Implements IDisposable
Private disposedValue As Boolean = False
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' Dispose managed state
Me._inputStream?.Dispose()
Me._inputStream = Nothing
Me._outputStream?.Dispose()
Me._outputStream = Nothing
End If
' Dispose unmanaged resources (like setting large fields to Nothing)
DisposeUnmanagedResources()
disposedValue = True
End If
End Sub
''' <summary>
''' Override this partial method to dispose of unmanaged resources, such as
''' setting large fields to Nothing.
''' </summary>
Partial Private Sub DisposeUnmanagedResources()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
Dispose(disposing:=True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Try
Dispose(disposing:=False)
Finally
MyBase.Finalize()
End Try
End Sub
End Class
Further Example for Modules
The source generator also supports modules with the DisposeModuleFields() method:
Imports System.IO
' Modules are supported too
Partial Public Module ResourceManager
' Fields marked with <DisposeField> will be automatically disposed via DisposeModuleFields()
<DisposeField>
Public CurrentLogStream As FileStream
<DisposeField>
Private _cache As MemoryStream
' Regular field - won't be auto-disposed
Private _tempData As Byte()
Sub Initialize()
CurrentLogStream = New FileStream("app.log", FileMode.OpenOrCreate)
_cache = New MemoryStream()
_tempData = New Byte(2047) {}
End Sub
' You can add custom unmanaged resource disposal logic:
Private Sub DisposeUnmanagedResources()
' Add custom disposal logic for unmanaged resources here
_tempData = Nothing
End Sub
End Module
For modules, the source generator creates:
' Generated file (ResourceManager_Disposers.g.vb)
Partial Public Module ResourceManager
''' <summary>
''' Disposes of all fields marked with <c><DisposeField></c> in the ResourceManager module.
''' </summary>
Public Sub DisposeModuleFields()
' Dispose both managed and unmanaged resources in a module
ResourceManager.CurrentLogStream?.Dispose()
ResourceManager.CurrentLogStream = Nothing
ResourceManager._cache?.Dispose()
ResourceManager._cache = Nothing
DisposeUnmanagedResources()
End Sub
''' <summary>
''' Override this partial method to dispose of unmanaged resources, such as
''' setting large fields to Nothing.
''' </summary>
Private Sub DisposeUnmanagedResources()
End Sub
End Module
To dispose of module resources, call the generated DisposeModuleFields() method:
' When cleaning up resources in your application
ResourceManager.DisposeModuleFields()
How It Works
The source generator scans your VB.NET code for:
- Partial classes, structures, or modules
- Fields marked with the
<DisposeField>attribute - Fields whose types implement
IDisposable
At compile time, it generates a partial class that implements the standard dispose pattern, ensuring that all marked fields are properly disposed when the containing object is disposed.
License
This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.