Woqqa.Enum2String
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Woqqa.Enum2String --version 1.0.0
NuGet\Install-Package Woqqa.Enum2String -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Woqqa.Enum2String" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Woqqa.Enum2String" Version="1.0.0" />
<PackageReference Include="Woqqa.Enum2String" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Woqqa.Enum2String --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Woqqa.Enum2String, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Woqqa.Enum2String@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Woqqa.Enum2String&version=1.0.0
#tool nuget:?package=Woqqa.Enum2String&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Enum 2 String
Description
Enum2String is a minimalistic library that provides a simple yet powerful way to define custom string representations for enum values. With a single attribute and extension methods, you can easily map enum values to display names, localization strings, or any custom text representation.
Guide
- Enum string mapping
enum SomeEnum
{
first, // There is - first
[AsString()] // AsString() → "none"
second,
[AsString("Third element")] // AsString() → "Third element"
third
}
- String get
AsString() and AsStringAttribute guide:
- The
AsStringAttributehas 2 constructors: one with a value parameter and one without. If no value is provided,"none"will be used. - The
AsString()method returns the value set viaAsStringAttributeor the raw field name from the enum.
enum Role
{
User,
[AsString()]
Moder,
[AsString("Administrator")]
Admin
}
var user = Role.User;
var moder = Role.Moder;
var admin = Role.Admin;
Console.WriteLine(user.AsString()); // User
Console.WriteLine(moder.AsString()); // none
Console.WriteLine(admin.AsString()); // Administrator
HasString() guide:
- The
HasString()method returns:trueif the value was set usingAsStringAttribute.falseif the value was not set.
enum SomeEnum
{
first,
[AsString("2nd element")]
second,
third
}
bool f = SomeEnum.first.HasString(); //false
bool f = SomeEnum.second.HasString(); //true
bool f = SomeEnum.third.HasString(); //false
TryAsString() guide:
- The
TryAsString(out string name)method returns:trueif the value was set usingAsStringAttribute.falseif it was not set. Additionally, it writes theAsStringAttributevalue to theout string nameparameter.
enum Settings
{
[AsString()]
Low,
[AsString("Medium graphic")]
Medium,
High
}
bool low = Settings.Low.TryAsString(out string low_name);
bool mid = Settings.Medium.TryAsString(out string mid_name);
bool high = Settings.High.TryAsString(out string high_name);
/*
low is true
mid is true
high is false
low_name is "none"
mid_name is "Medium graphic"
high_name is "High"
*/
Installation
- .NET CLI:
dotnet add package Woqqa.Enum2String --version 1.0.0
- Package Manager:
Install-Package Woqqa.Enum2String
- Reference:
<PackageReference Include="Woqqa.Enum2String" Version="1.0.0" />
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- No dependencies.
-
net9.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.