Raiqub.Generators.EnumUtilities 1.4.2

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Raiqub.Generators.EnumUtilities --version 1.4.2                
NuGet\Install-Package Raiqub.Generators.EnumUtilities -Version 1.4.2                
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.4.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Raiqub.Generators.EnumUtilities --version 1.4.2                
#r "nuget: Raiqub.Generators.EnumUtilities, 1.4.2"                
#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.
// Install Raiqub.Generators.EnumUtilities as a Cake Addin
#addin nuget:?package=Raiqub.Generators.EnumUtilities&version=1.4.2

// Install Raiqub.Generators.EnumUtilities as a Cake Tool
#tool nuget:?package=Raiqub.Generators.EnumUtilities&version=1.4.2                

Enum Utilities

GitHub license Nuget Nuget

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

🏃 Quickstart   |   📗 Guide   |   📦 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.

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)
    • IsDefined(this Categories)
  • CategoriesFactory
    • TryParse(string?, StringComparison, out Categories)
    • TryParseIgnoreCase(string?, out Categories)
    • TryParse(string?, out Categories)
    • TryParse(string?, StringComparison)
    • TryParseIgnoreCase(string?)
    • TryParse(string?)
    • GetValues()
    • GetNames()
  • CategoriesValidation
    • IsDefined(Categories)
    • IsDefined(string?, StringComparison)
    • IsDefinedIgnoreCase(string?)
    • IsDefined(string?)

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

Guide

The following attributes are supported:

EnumMemberAttribute

Example:

[EnumGenerator]
public enum PaymentMethod
{
    [EnumMember(Value = "Credit card")]
    Credit,
    [EnumMember(Value = "Debit card")]
    Debit,
    Cash,
    Cheque
}

This will generate the following methods:

  • PaymentMethodExtensions
    • ToEnumMemberValue(this PaymentMethod)
  • PaymentMethodFactory
    • TryParseFromEnumMemberValue(string?, StringComparison, out PaymentMethod)
    • TryParseFromEnumMemberValue(string?, out PaymentMethod)
    • TryParseFromEnumMemberValue(string?, StringComparison)
    • TryParseFromEnumMemberValue(string?)

DescriptionAttribute

Example:

[EnumGenerator]
public enum PaymentMethod
{
    Credit,
    Debit,
    [Description("The payment by using physical cash")]
    Cash,
    Cheque
}

This will generate the following methods:

  • PaymentMethodExtensions
    • GetDescription(this PaymentMethod)
  • PaymentMethodFactory
    • TryCreateFromDescription(string?, StringComparison, out PaymentMethod)
    • TryCreateFromDescription(string?, out PaymentMethod)
    • TryCreateFromDescription(string?, StringComparison)
    • TryCreateFromDescription(string?)

DisplayAttribute

Example:

[EnumGenerator]
public enum WeekDays
{
    [Display(
        Name = nameof(Strings.MondayFull),
        ShortName = nameof(Strings.MondayShort),
        Description = nameof(Strings.MondayDescription),
        ResourceType = typeof(Strings))]
    Monday,
    [Display(ShortName = "Tue")]
    Tuesday,
    [Display]
    Wednesday,
    [Display(Name = "Thursday")]
    Thursday,
    [Display(Name = "Friday", ShortName = "Fri")]
    Friday,
    [Display(ShortName = "Sat", Description = "Almost the last day of the week")]
    Saturday,
    [Display(Description = "The last day of the week")]
    Sunday
}

Note that if ResourceType is provided the generated code will correctly get the value from resource.

This will generate the following methods:

  • WeekDaysExtensions
    • GetDisplayShortName(this WeekDays)
    • GetDisplayName(this WeekDays)
    • GetDescription(this WeekDays)
  • WeekDaysFactory
    • TryCreateFromDisplayShortName(string?, StringComparison, out WeekDays)
    • TryCreateFromDisplayShortName(string?, out WeekDays)
    • TryCreateFromDisplayShortName(string?, StringComparison)
    • TryCreateFromDisplayShortName(string?)
    • TryCreateFromDisplayName(string?, StringComparison, out WeekDays)
    • TryCreateFromDisplayName(string?, out WeekDays)
    • TryCreateFromDisplayName(string?, StringComparison)
    • TryCreateFromDisplayName(string?)
    • TryCreateFromDescription(string?, StringComparison, out WeekDays)
    • TryCreateFromDescription(string?, out WeekDays)
    • TryCreateFromDescription(string?, StringComparison)
    • TryCreateFromDescription(string?)

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 was computed.  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 was computed.  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. 
.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

    • 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.9.2 262 10/6/2024
1.8.31 765 8/31/2024
1.8.29 292 8/25/2024
1.7.1 452 7/8/2024
1.6.44 1,829 5/19/2024
1.6.14 736 4/6/2024
1.6.1 373 3/10/2024
1.5.1 253 2/26/2024
1.4.2 774 12/30/2023
1.3.11 344 12/24/2023
1.2.2 1,165 9/14/2023
1.1.1 487 7/22/2023
1.0.1 404 7/22/2023
1.0.0 1,113 1/28/2023