Labeler.Core
0.1.0-alpha
dotnet add package Labeler.Core --version 0.1.0-alpha
NuGet\Install-Package Labeler.Core -Version 0.1.0-alpha
<PackageReference Include="Labeler.Core" Version="0.1.0-alpha" />
paket add Labeler.Core --version 0.1.0-alpha
#r "nuget: Labeler.Core, 0.1.0-alpha"
// Install Labeler.Core as a Cake Addin #addin nuget:?package=Labeler.Core&version=0.1.0-alpha&prerelease // Install Labeler.Core as a Cake Tool #tool nuget:?package=Labeler.Core&version=0.1.0-alpha&prerelease
Labeler
Labeler is a NuGet package that allows for adding Labels easily onto Enum fields to use when there is a need to present information quickly to a view in an elegant way.
Contents
The problem
Imagine having an enum where some of its fields consist of multiple words like the enum below:
enum SteakDoneness
{
Rare,
MediumRare,
Medium,
MediumWell,
WellDone,
}
In case we want to print an enum value we will have the following result which is not the most elegant:
Console.WriteLine($"I ordered a {SteakDoneness.MediumWell} steak.");
I ordered a MediumWell steak.
Installation
You can install the library by searching into the NuGet library or by running the following command:
PM> Install-Package Labeler.Core -Version 0.1.0-alpha
How to Use
Add the label attributes onto enum fields
enum SteakDoneness
{
Rare,
[Label("Medium Rare")]
MediumRare,
Medium,
[Label("Medium Well")]
MediumWell,
[Label("Well Done")]
[Label("Well")] // "Well Done" level can be descrbed just as "Well" as well...
WellDone,
}
As you can see we can have multiple label attributes decorating some fields, while totally ignoring others that don't need any labeling.
.GetLabel()
To get the Label's Value, simply just call the .GetLabel()
extention method on any enum type. Upon that, you can use methods that you would use upon a regular string value.
Example:
Console.WriteLine($"I ordered a {SteakDoneness.MediumWell.GetLabel().ToLower()} steak.");
I ordered a medium well steak.
In case there is no labeling, the enum's field name will be returned as string, as it would when using .ToString()
.
In case there are multiple labels decorating the enum's field, the first will be returned.
.GetLabels()
To get the Label's values as IEnumerable<string>
, simply call the .GetLabels()
extention method on any enum type. Upon that, you can use LINQ methods to get the first or last string label or apply your logic.
Example:
Console.WriteLine($"I ordered a {SteakDoneness.WellDone.GetLabels().First().ToLower()} steak.");
I ordered a well done steak.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.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 |
---|---|---|
0.1.0-alpha | 189 | 1/18/2021 |