Redpoint.StringEnum 2024.1177.305

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Redpoint.StringEnum --version 2024.1177.305
NuGet\Install-Package Redpoint.StringEnum -Version 2024.1177.305
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="Redpoint.StringEnum" Version="2024.1177.305" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Redpoint.StringEnum --version 2024.1177.305
#r "nuget: Redpoint.StringEnum, 2024.1177.305"
#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.
// Install Redpoint.StringEnum as a Cake Addin
#addin nuget:?package=Redpoint.StringEnum&version=2024.1177.305

// Install Redpoint.StringEnum as a Cake Tool
#tool nuget:?package=Redpoint.StringEnum&version=2024.1177.305

Redpoint.StringEnum

This library provides an API for defining and using string-backed enumeration types. These are useful when used with databases, where storing string values for enumerations improves forward and backward compatibility (unlike storing numeric values).

Defining a string-backed enumeration type

You can create a string-backed enumeration, by defining a class like so:

class Example : StringEnum<Example>
{
    public static readonly StringEnumValue<Example> FirstValue = Create("first-value");

    public static readonly StringEnumValue<Example> SecondValue = Create("second-value");

    public static readonly StringEnumValue<Example> ThirdValue = Create("third-value");

    // ...
}

The string values that represent the enumeration are case-and-byte sensitive (ordinal comparison). You can not create enumeration values from a null string; ArgumentNullException will be thrown if you pass a null value to Create.

Defining additional enumeration values at runtime

There is currently no API for defining additional enumeration values at runtime; the possible enumeration values are lazy-loaded once by looking at the static fields and static properties of the class that inherits from StringEnum<T> (via the T type parameter). Both public and non-public fields/properties are included.

This library is both trim and AOT-compatible, as it uses [DynamicallyAccessedMembers] to ensure the fields and properties of the enumeration type are available at runtime for reflection.

Static usage of values

In most code, you'll simply use the static readonly fields you've defined, such as Example.FirstValue. All instances of the value - even those from parsing - are guaranteed to be reference and value equal.

Parsing known values from strings

To parse a value that is known to be valid from a string, you can use Example.Parse or StringEnumValue<Example>.Parse:

var value = Example.Parse("first-value");
var value2 = StringEnumValue<Example>.Parse("second-value");

If the value is not valid when calling Parse, StringEnumParseException will be thrown. You should use TryParse (see below) if the value is potentially invalid.

If you pass a null value to Parse, ArgumentNullException will be thrown.

Parsing potentially invalid string values

If you're unsure whether a string is a valid enumeration value, you can use Example.TryParse or StringEnumValue<Example>.TryParse:

var isValid = Example.TryParse("...", out var value);
var isValid2 = StringEnumValue<Example>.TryParse("...", out var value2);

If you pass a null value to TryParse, ArgumentNullException will be thrown.

Parsing strings to runtime-based enumeration types

If you don't know the type of the string enumeration you want to parse into at compile time, you can use DynamicStringEnumValue.TryParse to parse any string value into an instance of StringEnumValue<T> by passing the StringEnumValue<T> type as the first parameter, like so:

var type = typeof(StringEnumValue<Example>);
var isValid = DynamicStringEnumValue.TryParse(type, "...", out var value);

You can use DynamicStringEnumValue.IsStringEnumValueType to check if type is a constructed generic type of StringEnumValue<T>. The APIs provided by DynamicStringEnumValue are primarily intended to be used with database serialization and deserialization, where the concrete types of values are not known at the point of deserialization.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Redpoint.StringEnum:

Package Downloads
Redpoint.CloudFramework The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2024.1177.305 71 6/25/2024
2024.1168.700 82 6/16/2024
2024.1168.649 73 6/16/2024
2024.1168.535 73 6/16/2024
2024.1167.643 76 6/15/2024
2024.1166.989 73 6/14/2024
2024.1166.818 71 6/14/2024
2024.1166.535 72 6/14/2024
2024.1166.422 68 6/14/2024
2024.1166.362 66 6/14/2024
2024.1166.360 72 6/14/2024
2024.1165.1212 69 6/13/2024
2024.1165.1160 68 6/13/2024
2024.1165.1019 74 6/13/2024
2024.1165.978 65 6/13/2024
2024.1165.920 65 6/13/2024
2024.1165.908 70 6/13/2024
2024.1165.873 71 6/13/2024
2024.1165.830 71 6/13/2024
2024.1165.806 71 6/13/2024
2024.1165.794 71 6/13/2024
2024.1165.792 73 6/13/2024
2024.1165.761 70 6/13/2024
2024.1165.743 68 6/13/2024
2024.1165.735 74 6/13/2024
2024.1165.713 73 6/13/2024
2024.1165.640 68 6/13/2024
2024.1165.574 73 6/13/2024
2024.1165.466 71 6/13/2024
2024.1165.417 72 6/13/2024
2024.1165.392 69 6/13/2024
2024.1164.341 61 6/12/2024
2024.1155.941 78 6/3/2024
2024.1140.783 83 5/19/2024
2024.1140.692 75 5/19/2024
2024.1140.673 76 5/19/2024
2024.1129.506 76 5/8/2024
2024.1128.831 95 5/7/2024
2024.1124.465 58 5/3/2024
2024.1123.1162 49 5/2/2024
2024.1123.1117 44 5/2/2024
2024.1123.1050 46 5/2/2024
2024.1122.352 73 5/1/2024
2024.1122.233 76 5/1/2024
2024.1115.1101 83 4/24/2024
2024.1115.1048 68 4/24/2024
2024.1115.932 72 4/24/2024
2024.1115.858 74 4/24/2024
2024.1115.665 80 4/24/2024
2024.1115.648 71 4/24/2024
2024.1115.591 78 4/24/2024
2024.1101.390 80 4/10/2024
2024.1100.741 82 4/9/2024
2024.1099.526 86 4/8/2024
2024.1099.466 80 4/8/2024
2024.1098.573 86 4/7/2024
2024.1095.977 85 4/4/2024
2024.1095.847 81 4/4/2024
2024.1095.604 83 4/4/2024
2024.1095.485 89 4/4/2024
2024.1094.930 84 4/3/2024
2024.1094.910 93 4/3/2024
2024.1094.387 95 4/3/2024
2024.1094.313 80 4/3/2024
2024.1093.520 84 4/2/2024
2024.1092.240 79 4/1/2024
2024.1089.1425 96 3/29/2024
2024.1089.983 80 3/29/2024
2024.1089.942 67 3/29/2024
2024.1089.2 87 3/29/2024
2024.1088.1343 82 3/28/2024
2024.1088.1139 82 3/28/2024
2024.1088.875 69 3/28/2024
2024.1088.700 74 3/28/2024
2024.1088.603 85 3/28/2024
2024.1088.590 90 3/28/2024
2024.1088.575 80 3/28/2024
2024.1088.561 83 3/28/2024
2024.1088.506 88 3/28/2024
2024.1088.18 87 3/28/2024
2024.1087.1181 96 3/27/2024
2024.1087.1173 88 3/27/2024
2024.1087.1143 95 3/27/2024
2024.1087.1108 88 3/27/2024
2024.1087.950 85 3/27/2024
2024.1087.939 82 3/27/2024
2024.1087.252 90 3/27/2024
2024.1070.736 86 3/10/2024
2024.1049.1437 94 2/19/2024
2024.1036.571 93 2/5/2024
2024.1036.508 76 2/5/2024
2024.1032.1204 80 2/1/2024
2024.1032.1133 79 2/1/2024
2024.1032.1122 78 2/1/2024
2024.1032.1071 77 2/1/2024
2024.1032.940 73 2/1/2024
2024.1031.912 80 1/31/2024
2024.1031.893 76 1/31/2024
2024.1031.851 65 1/31/2024
2024.1031.829 76 1/31/2024
2024.1031.386 76 1/31/2024
2024.1031.318 68 1/31/2024
2024.1017.460 97 1/17/2024
2024.1017.420 76 1/17/2024
2024.1017.163 83 1/17/2024
2024.1016.414 251 1/16/2024
2024.1012.167 88 1/12/2024
2024.1011.406 89 1/11/2024
2024.1010.315 102 1/10/2024
2023.1358.326 134 12/24/2023
2023.1351.473 97 12/17/2023
2023.1351.445 90 12/17/2023
2023.1351.432 99 12/17/2023
2023.1350.478 99 12/16/2023
2023.1344.600 113 12/10/2023
2023.1344.529 114 12/10/2023
2023.1339.582 113 12/5/2023
2023.1338.1072 119 12/4/2023
2023.1336.844 110 12/2/2023
2023.1335.378 107 12/1/2023
2023.1335.236 105 12/1/2023
2023.1335.203 96 12/1/2023