Generates string properties to access enum descriptions on marked types. Primarily used to simplify binding enum description text in UI elements and avoid runtime reflection. Also allows description access via extension method for targeted enums.
$ dotnet add package EnumScribeAn easy-to-use source generator providing efficient access to enum description text.
When binding an enum to a UI component, it's uncommon to be able to display the enum identifier itself, EG. "OutOfStock" doesn't adhere to english grammar. Working around this usually either comes with a runtime cost (reflection) or maintainability cost (manual enum -> text mapping)
EnumScribe simplifies the process by generating the mapping at compile time based on Description attributes and exposing it via properties added to the type, making the text easier to consume in most UI frameworks.
NoScribeJsonIgnore (Supports Json.NET and System.Text.Json)DescriptionText() extension method (included in the EnumScribe.Extensions namespace)using EnumScribe;
// Source code
[Scribe]
public partial class MyDto
{
public MyEnum? ToProperty { get; set; }
public MyEnum ToMethod { get; set; }
public partial string ToMethodDescription();
[NoScribe]
public MyEnum HiddenProperty { get; set; }
}
// Generated code
public partial class MyDto
{
// "ToProperty" + "Description"
public string? ToPropertyDescription { get { /* ... */ } }
// "ToMethod" + "Description"
public partial string ToMethodDescription() { /* ... */ }
}
Source generators are a relatively new feature and are still facing some intermittent teething issues (EG. #49249)
Most issues are addressed by cleaning the project, then restarting your IDE and rebuilding it.
It can be helpful to opt into outputting generated files by including the below snippet in the consuming project. While these are normally viewable in the VS solution explorer (Dependencies\Analyzers\EnumScribe) it's not entirely reliable yet.
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<!-- MyProject\obj\GeneratedFiles by default -->
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
Compiler errors CS1061 and CS8795 warns of missing or unimplemented type members and may erronously report that generated code is missing. This may presist through project rebuilds, but can usually be resolved by restarting your IDE.
IIncrementalGenerator)T in generic types where T is an enumReScribe to manually override scribe rules on a property/field basis (suffix, accessibility, json ignore)ReScribe attribute on property without the containing class requiring ScribeEnum