Raiqub.Generators.EnumUtilities
1.0.0
dotnet add package Raiqub.Generators.EnumUtilities --version 1.0.0
NuGet\Install-Package Raiqub.Generators.EnumUtilities -Version 1.0.0
<PackageReference Include="Raiqub.Generators.EnumUtilities" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
paket add Raiqub.Generators.EnumUtilities --version 1.0.0
#r "nuget: Raiqub.Generators.EnumUtilities, 1.0.0"
// Install Raiqub.Generators.EnumUtilities as a Cake Addin
#addin nuget:?package=Raiqub.Generators.EnumUtilities&version=1.0.0
// Install Raiqub.Generators.EnumUtilities as a Cake Tool
#tool nuget:?package=Raiqub.Generators.EnumUtilities&version=1.0.0
Enum Utilities
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 follwing methods:
- CategoriesExtensions
- ToStringFast(this Categories)
- IsDefined(this Categories)
- CategoriesFactory
- TryParse(string?, StringComparison, out Categories)
- TryParseIgnoreCase(string?, out Categories)
- TryParse(string?, out Categories)
- 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)
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)
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)
- TryCreateFromDisplayName(string?, StringComparison, out WeekDays)
- TryCreateFromDescription(string?, StringComparison, out WeekDays)
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 | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.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 | 89 | 1/28/2023 |