EnumStringValues 4.0.2

.NET Standard 2.0 .NET Framework 4.0
Install-Package EnumStringValues -Version 4.0.2
dotnet add package EnumStringValues --version 4.0.2
<PackageReference Include="EnumStringValues" Version="4.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnumStringValues --version 4.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EnumStringValues, 4.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install EnumStringValues as a Cake Addin
#addin nuget:?package=EnumStringValues&version=4.0.2

// Install EnumStringValues as a Cake Tool
#tool nuget:?package=EnumStringValues&version=4.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

EnumStringValues

Library to allow conversion between an Enum Value and a string, in both directions. Implemented as an Attribute to be applied to Enum fields to define a string, and methods to extract the defined string given the enum or provide the matching given a string. Enum name is registered as a default stringValue everywhere. All operations are cached, to save on reflection overheads. But this can be disabled, with EnumExtensions.Behaviour.UseCaching, if desired.

Breaking Change Log (3.0 → 4.0)

  • .NET 3.5 support is entirely dropped. Please use the last 3.2.* build.
  • There are 2 changes which change behaviour of the library, though do not cause compile-time errors. They are:
    • Caching.
      • This is now active by default, which will change the CPU vs RAM profile of EnumStringValues. See docs below for how to disable caching, if desired.
    • Use of Enum Literal Name.
      • The literal name is now always included by default, which might affect behaviour in some edge cases.
      • Again, this behaviour is controllable, see docs below for how to adjust this behaviour, if desired.

Breaking Change Log (2.0 → 3.0)

The Deprecated ParseStringValueToEnum method has been removed. Please use ParseToEnum instead.

Example Usage

For full Documentation, please see the GitHub page for the project.

/* Define Mappings. */
public enum exampleEnum
{
  EnumWithoutAnyCustomStringValue,

  [StringValue("AValue")]
  EnumWithAStringValueDefined,

  [StringValue("2", true),
   StringValue("Two")]
  EnumWithMultipleStringValuesDefinedAndOneMarkedAsPreferred
}


/* Map from Enum to string. */
using EnumStringValues.EnumExtensions;

exampleEnum.EnumWithoutAnyCustomStringValue.GetStringValue()
               // returns "EnumWithoutAnyCustomStringValue"

exampleEnum.EnumWithAStringValueDefined.GetStringValue()
               // returns "AValue"

exampleEnum.EnumWithMultipleStringValueDefinedAndOneMarkedAsPreferred.GetStringValue() 
               // returns "2"


/* Map from string to Enum. */
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()
               // returns exampleEnum.EnumWithoutAnyCustomStringValue

("AValue").ParseToEnum<exampleEnum>()
               // returns exampleEnum.EnumWithAStringValueDefined

("2").ParseToEnum<exampleEnum>()
               // returns exampleEnum.EnumWithMultipleStringValuesDefinedAndOneMarkedAsPreferred

("Two").ParseToEnum<exampleEnum>()
               // also returns exampleEnum.EnumWithMultipleStringValuesDefinedAndOneMarkedAsPreferred


/* Enable Caching. */
EnumExtensions.Behaviour.UseCaching = true;
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()
               // Does Work
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()
               // Does not do Work, as result is already cached.
EnumExtensions.Behaviour.UseCaching = false;
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()
               // Returns to doing Work again


/* Modify behavior with regard underlying enum name. */
EnumExtensions.Behaviour.ShouldIncludeUnderlyingName = UnderlyingNameUsed.Never;
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()               // Fails
 exampleEnum.EnumWithoutAnyCustomStringValue.GetStringValue()                // returns null
("EnumWithAStringValueDefined").ParseToEnum<exampleEnum>()                   // Fails
 exampleEnum.EnumWithAStringValueDefined.GetAllStringValues()                // returns only "AValue"

EnumExtensions.Behaviour.ShouldIncludeUnderlyingName = UnderlyingNameUsed.IfNoOverrideGiven;
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()               // Suceeds
 exampleEnum.EnumWithoutAnyCustomStringValue.GetStringValue()                // returns "EnumWithoutAnyCustomStringValue"
("EnumWithAStringValueDefined").ParseToEnum<exampleEnum>()                   // Fails
 exampleEnum.EnumWithAStringValueDefined.GetAllStringValues()                // returns only "AValue"

EnumExtensions.Behaviour.ShouldIncludeUnderlyingName = UnderlyingNameUsed.Always;
("EnumWithoutAnyCustomStringValue").ParseToEnum<exampleEnum>()               // Suceeds
 exampleEnum.EnumWithoutAnyCustomStringValue.GetStringValue()                // returns "EnumWithoutAnyCustomStringValue"
("EnumWithAStringValueDefined").ParseToEnum<exampleEnum>()                   // Suceeds
 exampleEnum.EnumWithAStringValueDefined.GetAllStringValues()                // returns only "AValue" and "EnumWithAStringValueDefined"
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
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on EnumStringValues:

Package Downloads
SanNuo.ERP.WebApis.Domain.Common

Package Description

IntisTelecom

UK Intis Telecom LTD Company number 11767499 100 New Bond Street First Floor, London, United Kingdom, W1S 1SP VAT: GB323461428

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.2 67,150 1/5/2020
4.0.1 341 1/2/2020
4.0.0 29,293 5/28/2019
3.2.0 511 5/28/2019
3.1.0 494 5/28/2019
3.0.1 483 5/28/2019
3.0.0 45,223 12/9/2017
2.0.0 11,249 5/29/2015
1.0.0 1,242 7/13/2014
0.9.1 930 7/13/2014
0.9.0 1,195 4/15/2014

Version 4.0.2
      - Re-Release without DEBUG enabled (4.0.1 was unintentionally a DEBUG build)
   Version 4.0.1
      - Fix MemoryLeak bug, in a MaliciousUser edge-case.
   Version 4.0.0
      - Change default behaviours.
         - Caching is enabled by default.
         - Enum literal name is AlwaysIncluded by default.
         - See docs and release notes for versions 3.1.0 and 3.2.0, to change these setting.
      - Add thread-safety.
         - Caching dictionaries were not thread-safe; now they are.
      - Drop support for .NET frameworks prior to .NET 4
         - Required, in order to implement thread-safety.
         - Note that version 3.2.0 does support .NET 3.5 and has all the same functionality available, but without thread-safety and with different default Behaviour.
         - Bug fixes will be applied to v 3.2.* but no new features will support .NET 3.5. Please contact me if you wish to argue otherwise.
   Version 3.2.0
      - Add direct control over how the enum's literal name is used.
         - By default behaviour remains the same - literal name is used if and only if there are no StringValue attributes defined.
         - Option is added to allow for literal name to always be included (as lowest priority) or never be included.
         - Control is modified via: `EnumExtensions.Behaviour.ShouldIncludeUnderlyingName = UnderlyingNameUsed.Always`
         - Default will be changed to 'Always' in v4.0
   Version 3.1.0
      - Add option of caching all work done from all end points.
         - Caching is opt-in; accessed by setting EnumExtensions.Behaviour.UseCaching = true.
         - Caching is NOT thread-safe (to maintain .NET 3.5 support)
         - Thread-safety will be added in v4.0
   Version 3.0.1
      - Update codebase to C# 7.3 and thus add System.Enum constraints on all typed methods.
         - Only impact should be to convert run-time errors into compile-time errors
   Version 3.0
      - Convert the project to .Net Standard 2.0
      - Remove the Obsolete `ParseStringValueToEnum` method. Please use `ParseToEnum`