EnumScribe 1.1.1
dotnet add package EnumScribe --version 1.1.1
NuGet\Install-Package EnumScribe -Version 1.1.1
<PackageReference Include="EnumScribe" Version="1.1.1" />
paket add EnumScribe --version 1.1.1
#r "nuget: EnumScribe, 1.1.1"
// Install EnumScribe as a Cake Addin
#addin nuget:?package=EnumScribe&version=1.1.1
// Install EnumScribe as a Cake Tool
#tool nuget:?package=EnumScribe&version=1.1.1
EnumScribe
An easy-to-use source generator providing efficient access to enum description text.
Why?
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.
Features
- Create additional properties returning the matching enum's description
- Implement available partial methods instead of creating new properties
- Customise the default suffix ("Description")
- Selectively scribe enums by accessibility and member type
- Ignore individual enums with
NoScribe
- Opt out of serializing generated properties with
JsonIgnore
(Supports Json.NET and System.Text.Json) - Access descriptions for scribed enums directly by using the
DescriptionText()
extension method (included in theEnumScribe.Extensions
namespace)
Basic usage
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() { /* ... */ }
}
Troubleshooting
No code is being generated :(
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'">
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
Definition cannot be found
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.
Planned work
- Update to source generators 2.0 (
IIncrementalGenerator
) - Proper tests (pending package update)
- Benchmarks
- Localise diagnostic text
- Scribe
T
in generic types whereT
is an enum - New attribute
ReScribe
to manually override scribe rules on a property/field basis (suffix, accessibility, json ignore) - Acknowledge
ReScribe
attribute on property without the containing class requiring ScribeEnum - Scribe functionality for structs and record structs
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.
See https://github.com/TonuFish/EnumScribe/tree/master/CHANGELOG.md for release notes.