RSCG – CredFetoEnum
RSCG – CredFetoEnum
name | CredFetoEnum |
nuget | https://www.nuget.org/packages/Credfeto.Enumeration.Source.Generation/ |
link | https://github.com/credfeto/credfeto-enum-source-generation |
author | Mark Ridgwell |
Enum / description to string
This is how you can use CredFetoEnum .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Credfeto.Enumeration.Source.Generation" Version="1.1.0.138" OutputItemType="Analyzer"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
using EnumClassDemo; Console.WriteLine(Colors.None.GetName()); Console.WriteLine(Colors.None.GetDescription());
using System.ComponentModel; namespace EnumClassDemo; public enum Colors { [Description("This should be never seen")] None =0, Red, Green, Blue, }
The code that is generated is
using System; using System.CodeDom.Compiler; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace EnumClassDemo; [GeneratedCode(tool: "Credfeto.Enumeration.Source.Generation.EnumGenerator", version: "1.1.0.138+a4e45a10ca3da5e916ae17843913bfff8c33cdef")] public static class ColorsGeneratedExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string GetName(this Colors value) { return value switch { Colors.None => nameof(Colors.None), Colors.Red => nameof(Colors.Red), Colors.Green => nameof(Colors.Green), Colors.Blue => nameof(Colors.Blue), _ => ThrowInvalidEnumMemberException(value: value) }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string GetDescription(this Colors value) { return value switch { Colors.None => "This should be never seen", _ => GetName(value) }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsDefined(this Colors value) { return value is Colors.None or Colors.Red or Colors.Green or Colors.Blue; } public static string ThrowInvalidEnumMemberException(this Colors value) { #if NET7_0_OR_GREATER throw new UnreachableException(message: "Colors: Unknown enum member"); #else throw new ArgumentOutOfRangeException(nameof(value), actualValue: value, message: "Unknown enum member"); #endif } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/CredFetoEnum
Leave a Reply