BB84.SourceGenerators 1.3.412

There is a newer version of this package available.
See the version list below for details.
dotnet add package BB84.SourceGenerators --version 1.3.412
                    
NuGet\Install-Package BB84.SourceGenerators -Version 1.3.412
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BB84.SourceGenerators" Version="1.3.412">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BB84.SourceGenerators" Version="1.3.412" />
                    
Directory.Packages.props
<PackageReference Include="BB84.SourceGenerators">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BB84.SourceGenerators --version 1.3.412
                    
#r "nuget: BB84.SourceGenerators, 1.3.412"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BB84.SourceGenerators@1.3.412
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BB84.SourceGenerators&version=1.3.412
                    
Install as a Cake Addin
#tool nuget:?package=BB84.SourceGenerators&version=1.3.412
                    
Install as a Cake Tool

BB84.SourceGenerators

A collection of C# source generators that automatically generate boilerplate code at compile time, reducing manual coding and improving code maintainability.

CI CD CodeQL Dependabot NuGet

License: MIT C# .NET Standard 2.0 Issues Commit RepoSize Release

Features

This package provides four powerful source generators:

  • Enumerator Extensions Generator - Fast, allocation-free extension methods for enums
  • Notification Properties Generator - Automatic INotifyPropertyChanged/INotifyPropertyChanging implementation
  • Abstraction Generator - Interface and implementation generation for static classes
  • INI File Generator - Compile-time INI file serialization and deserialization

Installation

Install the package via NuGet:

dotnet add package BB84.SourceGenerators

Or via Package Manager Console:

Install-Package BB84.SourceGenerators

Usage

1. Enumerator Extensions Generator

Generates high-performance extension methods for enumerations, providing faster alternatives to Enum.ToString(), Enum.IsDefined(), Enum.GetNames(), and Enum.GetValues().

Attribute
[GenerateEnumeratorExtensions]
Example
using BB84.SourceGenerators.Attributes;

[GenerateEnumeratorExtensions]
public enum Status
{
    [System.ComponentModel.Description("Pending approval")]
    Pending = 0,
    Active = 1,
    [System.ComponentModel.Description("Temporarily inactive")]
    Inactive = 2,
    Deleted = 3
}
Generated Methods

The generator creates the following extension methods:

  • ToStringFast() - Returns the name of the enum value as a string
  • IsDefinedFast(this TEnum value) - Checks if an enum value is defined
  • IsDefinedFast(this string name) - Checks if an enum name is defined
  • GetNamesFast() - Returns all enum names as an IEnumerable<string>
  • GetValuesFast() - Returns all enum values as an IEnumerable<TEnum>
  • GetDescriptionFast() - Returns the description from [Description] attribute, or the name if not present
Usage Example
var status = Status.Pending;

// Fast string conversion
string name = status.ToStringFast(); // "Pending"

// Check if defined
bool isDefined = status.IsDefinedFast(); // true
bool isNameDefined = "Active".IsDefinedFast(); // true

// Get description
string description = status.GetDescriptionFast(); // "Pending approval"

// Get all names and values
IEnumerable<string> names = status.GetNamesFast();
IEnumerable<Status> values = status.GetValuesFast();

2. Notification Properties Generator

Automatically generates properties with INotifyPropertyChanged and INotifyPropertyChanging support for private fields in a class.

Attribute
[GenerateNotifications(bool isChanged = false)]

Parameters:

  • isChanged - When true, generates an additional IsChanged boolean property that is set to true when any property changes
Example
using BB84.SourceGenerators.Attributes;

[GenerateNotifications(isChanged: true)]
public partial class Person
{
    private int _id;
    private string _name;
    private string _email;
    private DateTime _createdAt;
    private DateTime? _updatedAt;

    public Person(int id, string name, string email)
    {
        _id = id;
        _name = name;
        _email = email;
        _createdAt = DateTime.UtcNow;
    }
}
Generated Code

The generator creates:

  • Public properties for each private field with change notification
  • Implementation of INotifyPropertyChanged and INotifyPropertyChanging interfaces
  • PropertyChanged and PropertyChanging events
  • RaisePropertyChanged() and RaisePropertyChanging() protected virtual methods
  • Optional IsChanged property (when isChanged parameter is true)
Usage Example
var person = new Person(1, "John Doe", "john@example.com");

// Subscribe to change notifications
person.PropertyChanging += (s, e) => Console.WriteLine($"Property {e.PropertyName} is changing");
person.PropertyChanged += (s, e) => Console.WriteLine($"Property {e.PropertyName} has changed");

// Change a property - events will fire automatically
person.Name = "Jane Doe";
person.Email = "jane@example.com";

// Check if object has been modified (when isChanged: true)
if (person.IsChanged)
{
    Console.WriteLine("Person has been modified");
}

3. Abstraction Generator

Generates interface and implementation classes for static classes, making them testable through dependency injection.

Attribute
[GenerateAbstraction(Type targetType, Type abstractionType, Type implementationType, params string[] excludeMethods)]

Parameters:

  • targetType - The static class to generate an abstraction for
  • abstractionType - The interface type to generate
  • implementationType - The implementation class type to generate
  • excludeMethods - Optional array of method names to exclude from generation
Example
using BB84.SourceGenerators.Attributes;

// Generate abstraction for System.IO.File
[GenerateAbstraction(typeof(File), typeof(IFileProvider), typeof(FileProvider))]
public partial class FileProvider
{
}

public partial interface IFileProvider
{
}
Generated Code

The generator creates:

  • A complete interface (IFileProvider) with all public static methods from the target type
  • An implementation class (FileProvider) that implements the interface and delegates to the static class
  • XML documentation comments using <inheritdoc>
Usage Example
// In your service
public class DocumentService
{
    private readonly IFileProvider _fileProvider;

    public DocumentService(IFileProvider fileProvider)
    {
        _fileProvider = fileProvider;
    }

    public string ReadDocument(string path)
    {
        return _fileProvider.ReadAllText(path);
    }
}

// In your DI container setup
services.AddSingleton<IFileProvider, FileProvider>();

// In tests, you can mock IFileProvider
var mockFileProvider = new Mock<IFileProvider>();
mockFileProvider.Setup(x => x.ReadAllText(It.IsAny<string>())).Returns("test content");

4. INI File Generator

Generates static Read and Write methods for classes, enabling compile-time INI file serialization and deserialization based on decorated properties.

Attributes
[GenerateIniFile]
[GenerateIniFileSection(string? name = null)]
[GenerateIniFileValue(string? name = null)]

Parameters:

  • GenerateIniFile - Marks a class for INI file code generation
  • GenerateIniFileSection - Marks a property as an INI file section. The optional name parameter specifies the section name; if omitted, the property name is used
  • GenerateIniFileValue - Marks a property as a key-value pair within an INI section. The optional name parameter specifies the key name; if omitted, the property name is used

Supported Value Types: string, int, long, float, double, bool, decimal, DateTime

Example
using BB84.SourceGenerators.Attributes;

[GenerateIniFile]
public partial class AppConfig
{
    [GenerateIniFileSection("General")]
    public GeneralSection General { get; set; }

    [GenerateIniFileSection("Database")]
    public DatabaseSection Database { get; set; }
}

public class GeneralSection
{
    [GenerateIniFileValue("AppName")]
    public string AppName { get; set; }

    [GenerateIniFileValue("Version")]
    public int Version { get; set; }

    [GenerateIniFileValue("Debug")]
    public bool Debug { get; set; }
}

public class DatabaseSection
{
    [GenerateIniFileValue("Server")]
    public string Server { get; set; }

    [GenerateIniFileValue("Port")]
    public int Port { get; set; }

    [GenerateIniFileValue("Timeout")]
    public double Timeout { get; set; }
}
Generated Methods

The generator creates the following static methods on the decorated class:

  • Read(string content) - Parses an INI file string and returns a deserialized instance
  • Write(TClass instance) - Serializes an instance into an INI file string
Usage Example
// Reading an INI file
string iniContent = File.ReadAllText("config.ini");
AppConfig config = AppConfig.Read(iniContent);

Console.WriteLine(config.General.AppName); // "MyApp"
Console.WriteLine(config.Database.Port);   // 5432

// Modifying and writing back
config.General.Debug = false;
config.Database.Timeout = 60.0;

string output = AppConfig.Write(config);
File.WriteAllText("config.ini", output);

// Output:
// [General]
// AppName=MyApp
// Version=1
// Debug=False
//
// [Database]
// Server=localhost
// Port=5432
// Timeout=60

Requirements

  • .NET Standard 2.0 or higher
  • C# 7.3 or higher
  • Supports .NET Framework 4.7.2+, .NET Core 2.0+, .NET 5+, .NET 6+, .NET 7+, .NET 8+

Performance Benefits

Enum Extensions

The generated enum extension methods provide significant performance improvements over reflection-based Enum methods:

  • ToStringFast() - Avoids boxing and uses switch expressions
  • IsDefinedFast() - Compile-time switch instead of runtime reflection
  • GetNamesFast()/GetValuesFast() - Returns pre-allocated arrays instead of reflection

Notification Properties

  • Generates optimized property setters with inline equality checks
  • Avoids reflection overhead of PropertyChanged.Fody or similar tools
  • Compile-time code generation means zero runtime overhead

INI File Serialization

  • Generates direct string parsing and formatting code at compile time
  • Avoids runtime reflection or third-party INI parsing libraries
  • Uses CultureInfo.InvariantCulture for consistent cross-platform formatting

How It Works

Source generators run during compilation and generate additional C# source files that are compiled alongside your code. This means:

  • Zero runtime overhead - All code is generated at compile time
  • IntelliSense support - Generated code appears in Visual Studio IntelliSense
  • Debuggable - You can step through generated code during debugging
  • No reflection - Generated code uses direct method calls

Contributing

Contributions are welcome! Please feel free to submit a Pull Request but see the Conduct first.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Robert Peter Meyer (BoBoBaSs84)

Changelog

See releases for version history and changelog.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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.

Version Downloads Last Updated
2.2.614 220 6/14/2026
2.1.607 237 6/7/2026
2.0.529 165 5/29/2026
2.0.526 169 5/26/2026
2.0.524 123 5/24/2026
1.11.515 163 5/15/2026
1.10.507 155 5/7/2026
1.9.503 153 5/3/2026
1.9.430 128 4/30/2026
1.9.428 122 4/28/2026
1.8.426 141 4/26/2026
1.7.421 131 4/21/2026
1.6.419 121 4/19/2026
1.5.417 110 4/17/2026
1.4.416 109 4/16/2026
1.4.415 112 4/15/2026
1.3.413 128 4/13/2026
1.3.412 119 4/12/2026
1.2.410 130 4/10/2026
1.2.409.1 108 4/9/2026
Loading failed