Raiqub.Generators.EnumUtilities 1.11.4

Prefix Reserved
dotnet add package Raiqub.Generators.EnumUtilities --version 1.11.4
                    
NuGet\Install-Package Raiqub.Generators.EnumUtilities -Version 1.11.4
                    
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="Raiqub.Generators.EnumUtilities" Version="1.11.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Raiqub.Generators.EnumUtilities" Version="1.11.4" />
                    
Directory.Packages.props
<PackageReference Include="Raiqub.Generators.EnumUtilities" />
                    
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 Raiqub.Generators.EnumUtilities --version 1.11.4
                    
#r "nuget: Raiqub.Generators.EnumUtilities, 1.11.4"
                    
#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.
#addin nuget:?package=Raiqub.Generators.EnumUtilities&version=1.11.4
                    
Install Raiqub.Generators.EnumUtilities as a Cake Addin
#tool nuget:?package=Raiqub.Generators.EnumUtilities&version=1.11.4
                    
Install Raiqub.Generators.EnumUtilities as a Cake Tool

Enum Utilities

Build status OpenSSF Scorecard GitHub license Nuget Nuget

A source generator for C# that uses Roslyn to create extensions and parsers for enumerations

🏃 Quickstart   |   📖 Documentation   |   📦 NuGet

<hr />

A source generator for C# that uses Roslyn to create extensions and parsers for enumerations, allowing to get a value associated to enum member or parse back from attribute value to enum member. All code generated at compile time thus avoid using reflection or boilerplate code.

Compatibility

Raiqub.Generators.EnumUtilities runs with Roslyn compiler so does not introduce a new dependency to your project besides a library containing the EnumGenerator attribute.

It requires at least the .NET 6 SDK to run, but you can target earlier frameworks.

Documentation

This README aims to give a quick overview of some Raiqub Enum Utilities features. For deeper detail of available features, be sure also to check out Documentation Page.

Quickstart

Add the package to your application using

dotnet add package Raiqub.Generators.EnumUtilities

Adding the package will automatically add a marker attribute, [EnumGenerator], to your project.

To use the generator, add the [EnumGenerator] attribute to an enum. For example:

[EnumGenerator]
public enum Categories
{
   Electronics,
   Food,
   Automotive,
   Arts,
   BeautyCare,
   Fashion
}

This will generate 3 classes with the following methods:

  • CategoriesExtensions
    • ToStringFast(this Categories)
    • GetStringCount(this Categories)
    • IsDefined(this Categories)
    • InterlockedAdd(this ref Categories, int)
    • InterlockedDecrement(this ref Categories)
    • InterlockedIncrement(this ref Categories)
    • InterlockedCompareExchange(this ref Categories, Categories, Categories)
    • InterlockedExchange(this ref Categories, Categories)
  • CategoriesFactory
    • Parse(string, bool = false)
    • Parse(ReadOnlySpan<char>, bool = false)
    • ParseOrNull(string?, bool = false)
    • TryParse(string?, bool, out Categories)
    • TryParse(string?, out Categories)
    • TryParse(string?, bool = false)
    • TryParse(ReadOnlySpan<char>, bool, out Categories)
    • TryParse(ReadOnlySpan<char>, out Categories)
    • TryParse(ReadOnlySpan<char>, bool = false)
    • GetValues()
    • GetNames()
  • CategoriesValidation
    • IsDefined(Categories)
    • IsDefined(string?, StringComparison)
    • IsDefinedIgnoreCase(string?)
    • IsDefined(string?)

Bit flags enums are supported too:

[Flags]
[EnumGenerator]
public enum Colours
{
    Red = 1,
    Blue = 2,
    Green = 4,
}

Then 3 classes will be generated with the following methods:

  • ColoursExtensions
    • ToStringFast(this Colours)
    • HasFlagFast(this Colours, Colours)
    • GetStringCount(this Colours)
    • IsDefined(this Colours)
    • InterlockedAnd(this ref Colours, Colours)
    • InterlockedOr(this ref Colours, Colours)
    • InterlockedCompareExchange(this ref Colours, Colours, Colours)
    • InterlockedExchange(this ref Colours, Colours)
  • ColoursFactory
    • Parse(string, bool = false)
    • Parse(ReadOnlySpan<char>, bool = false)
    • ParseOrNull(string?, bool = false)
    • TryParse(string?, bool, out Categories)
    • TryParse(string?, out Categories)
    • TryParse(string?, bool = false)
    • TryParse(ReadOnlySpan<char>, bool, out Categories)
    • TryParse(ReadOnlySpan<char>, out Categories)
    • TryParse(ReadOnlySpan<char>, bool = false)
    • GetValues()
    • GetNames()
  • ColoursValidation
    • IsDefined(Colours)
    • IsDefined(string?, StringComparison)
    • IsDefinedIgnoreCase(string?)
    • IsDefined(string?)

All generated code are properly nullable annotated and removed from code coverage.

Supported members attributes

The following attributes are supported:

JSON Serialization

Besides the member name, supports the EnumMemberAttribute and JsonPropertyNameAttribute attributes.

Example:

[JsonConverterGenerator]
[JsonConverter(typeof(SeasonJsonConverter))]
public enum Season
{
    [EnumMember(Value = "\ud83c\udf31")]
    Spring = 1,
    [EnumMember(Value = "\u2600\ufe0f")]
    Summer,
    [EnumMember(Value = "\ud83c\udf42")]
    Autumn,
    [EnumMember(Value = "\u26c4")]
    Winter
}

This will generate the following JSON converter: SeasonJsonConverter.

Contributing

If something is not working for you or if you think that the source file should change, feel free to create an issue or Pull Request. I will be happy to discuss and potentially integrate your ideas!

License

See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.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.

Version Downloads Last updated
1.11.4 32 3/30/2025
1.10.2 230 2/10/2025
1.9.21 174 12/18/2024
1.9.2 628 10/6/2024
1.8.31 906 8/31/2024
1.8.29 404 8/25/2024
1.7.1 534 7/8/2024
1.6.44 2,037 5/19/2024
1.6.14 752 4/6/2024
1.6.1 379 3/10/2024
1.5.1 259 2/26/2024
1.4.2 784 12/30/2023
1.3.11 352 12/24/2023
1.2.2 1,171 9/14/2023
1.1.1 491 7/22/2023
1.0.1 409 7/22/2023
1.0.0 1,135 1/28/2023