Genbox.EnumSourceGen
1.0.0-alpha.4
Prefix Reserved
See the version list below for details.
dotnet add package Genbox.EnumSourceGen --version 1.0.0-alpha.4
NuGet\Install-Package Genbox.EnumSourceGen -Version 1.0.0-alpha.4
<PackageReference Include="Genbox.EnumSourceGen" Version="1.0.0-alpha.4" />
paket add Genbox.EnumSourceGen --version 1.0.0-alpha.4
#r "nuget: Genbox.EnumSourceGen, 1.0.0-alpha.4"
// Install Genbox.EnumSourceGen as a Cake Addin #addin nuget:?package=Genbox.EnumSourceGen&version=1.0.0-alpha.4&prerelease // Install Genbox.EnumSourceGen as a Cake Tool #tool nuget:?package=Genbox.EnumSourceGen&version=1.0.0-alpha.4&prerelease
EnumSourceGen
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
andIsFlagsEnum
is const to allow the compiler to fold constants.
- Supports name and description from DisplayAttribute.
- Only generates
GetDisplayName()
andGetDescription()
extensions when the attribute is present.
- Only generates
- Support for flag enums via the FlagsAttribute.
- Only generates
IsFlagSet()
when the attribute is present.
- Only generates
- 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
andDescription
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 | Versions 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. net9.0 was computed. 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. |
-
.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 | 120 | 3/5/2024 |
1.0.0-alpha.12 | 365 | 8/28/2023 |
1.0.0-alpha.11 | 103 | 8/13/2023 |
1.0.0-alpha.10 | 145 | 6/15/2023 |
1.0.0-alpha.9 | 136 | 4/10/2023 |
1.0.0-alpha.8 | 102 | 4/9/2023 |
1.0.0-alpha.7 | 104 | 4/9/2023 |
1.0.0-alpha.6 | 147 | 3/23/2023 |
1.0.0-alpha.5 | 127 | 3/22/2023 |
1.0.0-alpha.4 | 103 | 3/22/2023 |
1.0.0-alpha.3 | 108 | 3/22/2023 |
1.0.0-alpha.2 | 129 | 2/28/2023 |
1.0.0-alpha.1 | 111 | 2/28/2023 |