Genbox.EnumSourceGen 1.0.0-alpha.9

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of Genbox.EnumSourceGen.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Genbox.EnumSourceGen --version 1.0.0-alpha.9
NuGet\Install-Package Genbox.EnumSourceGen -Version 1.0.0-alpha.9
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="Genbox.EnumSourceGen" Version="1.0.0-alpha.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Genbox.EnumSourceGen --version 1.0.0-alpha.9
#r "nuget: Genbox.EnumSourceGen, 1.0.0-alpha.9"
#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 Genbox.EnumSourceGen as a Cake Addin
#addin nuget:?package=Genbox.EnumSourceGen&version=1.0.0-alpha.9&prerelease

// Install Genbox.EnumSourceGen as a Cake Tool
#tool nuget:?package=Genbox.EnumSourceGen&version=1.0.0-alpha.9&prerelease

EnumSourceGen

NuGet License

Description

A source generator to generate common methods for your enum types at compile-time. Print values, parse, or get the underlying value of enums without using reflection.

Features

  • Intuitive API with discoverability through IntelliSense. All enums can be accessed via the Enums class.
  • High-performance
    • Zero allocations whenever possible.
    • MemberCount and IsFlagsEnum is const to allow the compiler to fold constants.
  • Supports name and description from DisplayAttribute.
    • Only generates GetDisplayName() and GetDescription()extensions when the attribute is present.
  • Support for flag enums via the FlagsAttribute.
    • Only generates IsFlagSet() when the attribute is present.
  • Support for private/internal enums
  • Support for enums nested inside classes
  • Support for user-set underlying values such as long, uint, byte, etc.
  • Support for duplicate enum names from different namespaces
  • Support for enums that reside in the global namespace
  • Has several options to control namespace, class name and more for generated code. See Options section below for details.

Parse/TryParse methods

EnumSourceGen has some additional features compared to dotnet's Enum.Parse<T>() and Enum.TryParse<T>():

  • Supports StringComparison (defaults to ordinal comparison)
  • Supports parsing DisplayName and Description from DisplayAttribute
  • You can enable/disable Name, Value, DisplayName or Description parsing via a flags enum: Enums.MyEnum.TryParse("val", out MyEnum v, MyEnumFormat.Name | MyEnumFormat.DisplayName)

IsDefined method

The IsDefined method is different than the one provided by dotnet. It supports flags out of the box. Enums.MyEnum.IsDefined((MyEnum)42) and Enums.MyEnum.IsDefined(MyEnum.Value1 | MyEnum.Value3) both work.

Examples

For the code sections below, the following enum is defined:

[EnumSourceGen]
[Flags]
internal enum MyEnum
{
    [Display(Name = "Value1Name", Description = "Value1Description")]
    Value1 = 1,
    [Display(Name = "DisplayNameForValue2")]
    Value2 = 2,
    [Display(Description = "Description for Value3")]
    Value3 = 4
}
Extensions

The following extensions are auto-generated on MyEnum:

MyEnum e = MyEnum.Value1;

Console.WriteLine("String value: " + e.GetString());
Console.WriteLine("Underlying value: " + e.GetUnderlyingValue());
Console.WriteLine("Display name: " + e.GetDisplayName());
Console.WriteLine("Description: " + e.GetDescription());
Console.WriteLine("Has Value1 flag: " + e.IsFlagSet(MyEnum.Value1));

Output from the code above:

String value: Value1
Underlying value: 1
Display name: Value1Name
Description: Value1Description
Has Value1 flag: True
Enums class

Enums is a class that contains functionality for the auto-generated enum.

Console.WriteLine("Number of members: " + Enums.MyEnum.MemberCount);
Console.WriteLine("Parse: " + Enums.MyEnum.Parse("value1", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("TryParse success: " + Enums.MyEnum.TryParse("value1", out MyEnum val, StringComparison.OrdinalIgnoreCase) + " value: " + val);
Console.WriteLine("Is Value1 part of the enum: " + Enums.MyEnum.IsDefined(MyEnum.Value1));

PrintArray("Member names:", Enums.MyEnum.GetMemberNames());
PrintArray("Member values:", Enums.MyEnum.GetMemberValues());
PrintArray("Underlying values:", Enums.MyEnum.GetUnderlyingValues());
PrintArray("Display names:", Enums.MyEnum.GetDisplayNames());
PrintArray("Descriptions:", Enums.MyEnum.GetDescriptions());

PrintArray simply iterates an array and list the values on separate lines.

Output from the code above:

Number of members: 3
Parse: Value1
TryParse success: True value: Value1
Is Value1 part of the enum: True
Member names:
- Value1
- Value2
- Value3
Member values:
- Value1
- Value2
- Value3
Underlying values:
- 1
- 2
- 4
Display names:
- (Value1, Value1Name)
- (Value2, DisplayNameForValue2)
Descriptions:
- (Value1, Value1Description)
- (Value3, Description for Value3)

Options

EnumSourceGenAttribute have several options to control the behavior of the generated code. They are specified in the constructor.

[EnumSourceGen(Generate = Generate.ClassAndExtensions, ExtensionClassName = "MyEnumExtensions")]
internal enum MyEnum
{
    Value,
}
Generate

You can set 3 different values for this option:

  • ClassAndExtensions (default): Generates both a static Enums class member and extensions
  • ClassOnly: Only generate the static Enum class member
  • ExtensionsOnly: Only generate the enum extensions

It is best practice to only enable what you need to keep the size of your assemblies small.

ExtensionClassName

The generated extension class is partial by default. So if you want to combine extension from your own class and the autogenerated one, you can use this option to set the name to the same as your extensions class. Defaults to <EnumName>Extensions.

ExtensionClassNamespace

Use this to control which namespace the extensions class belongs to. Defaults to the namespace of the enum.

EnumsClassName

Use this to set the name of the Enums class to something else.

EnumsClassNamespace

Used this to specify the namespace for the Enums class. Defaults to the namespace of the enum.

EnumNameOverride

Sometimes you might have two enums named the same, but in different namespaces. You can use this option to override the name of the enum in the generated code. For example, if your enum is named MyEnum the Enums class can be accessed like this:

Enums.MyEnum.GetMemberNames()

If you set EnumNameOverride to OtherEnum it will look like this instead:

Enums.OtherEnum.GetMemberNames()
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.0.0-alpha.13 51 3/5/2024
1.0.0-alpha.12 258 8/28/2023
1.0.0-alpha.11 84 8/13/2023
1.0.0-alpha.10 111 6/15/2023
1.0.0-alpha.9 112 4/10/2023
1.0.0-alpha.8 84 4/9/2023
1.0.0-alpha.7 87 4/9/2023
1.0.0-alpha.6 127 3/23/2023
1.0.0-alpha.5 110 3/22/2023
1.0.0-alpha.4 88 3/22/2023
1.0.0-alpha.3 87 3/22/2023
1.0.0-alpha.2 112 2/28/2023
1.0.0-alpha.1 96 2/28/2023