Redpoint.StringEnum
2024.1360.354
Prefix Reserved
dotnet add package Redpoint.StringEnum --version 2024.1360.354
NuGet\Install-Package Redpoint.StringEnum -Version 2024.1360.354
<PackageReference Include="Redpoint.StringEnum" Version="2024.1360.354" />
paket add Redpoint.StringEnum --version 2024.1360.354
#r "nuget: Redpoint.StringEnum, 2024.1360.354"
// Install Redpoint.StringEnum as a Cake Addin #addin nuget:?package=Redpoint.StringEnum&version=2024.1360.354 // Install Redpoint.StringEnum as a Cake Tool #tool nuget:?package=Redpoint.StringEnum&version=2024.1360.354
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 | Versions 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. |
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Redpoint.StringEnum:
Package | Downloads |
---|---|
Redpoint.CloudFramework
A framework for building ASP.NET Core applications on top of Google Cloud Firestore in Datastore mode. Not only does this framework provide a model-based API for interacting with Google Cloud Firestore, it contains useful implementations of things like database migrations, distributed locks, geographic indexes and sharded counters. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2024.1360.354 | 70 | 12/25/2024 |
2024.1360.60 | 65 | 12/25/2024 |
2024.1360.32 | 65 | 12/25/2024 |
2024.1358.450 | 66 | 12/23/2024 |
2024.1358.359 | 67 | 12/23/2024 |
2024.1358.312 | 62 | 12/23/2024 |
2024.1358.244 | 71 | 12/23/2024 |
2024.1358.120 | 59 | 12/23/2024 |
2024.1348.253 | 76 | 12/13/2024 |
2024.1345.1379 | 65 | 12/10/2024 |
2024.1345.506 | 63 | 12/10/2024 |
2024.1345.379 | 62 | 12/10/2024 |
2024.1345.145 | 68 | 12/10/2024 |
2024.1345.72 | 65 | 12/10/2024 |
2024.1345.69 | 61 | 12/10/2024 |
2024.1345.15 | 73 | 12/10/2024 |
2024.1344.1436 | 68 | 12/10/2024 |
2024.1344.600 | 75 | 12/9/2024 |
2024.1344.411 | 74 | 12/9/2024 |
2024.1340.379 | 82 | 12/5/2024 |
2024.1340.15 | 73 | 12/5/2024 |
2024.1338.786 | 80 | 12/3/2024 |
2024.1338.722 | 85 | 12/3/2024 |
2024.1338.697 | 75 | 12/3/2024 |
2024.1338.639 | 78 | 12/3/2024 |
2024.1338.541 | 78 | 12/3/2024 |
2024.1338.318 | 67 | 12/3/2024 |
2024.1338.298 | 70 | 12/3/2024 |
2024.1338.98 | 68 | 12/3/2024 |
2024.1337.756 | 75 | 12/2/2024 |
2024.1337.634 | 82 | 12/2/2024 |
2024.1337.625 | 70 | 12/2/2024 |
2024.1337.621 | 71 | 12/2/2024 |
2024.1337.594 | 72 | 12/2/2024 |
2024.1337.136 | 59 | 12/2/2024 |
2024.1336.793 | 83 | 12/1/2024 |
2024.1336.773 | 80 | 12/1/2024 |
2024.1336.305 | 70 | 12/1/2024 |
2024.1336.12 | 85 | 12/1/2024 |
2024.1335.1421 | 80 | 11/30/2024 |
2024.1335.1384 | 77 | 11/30/2024 |
2024.1334.1308 | 77 | 11/29/2024 |
2024.1334.764 | 73 | 11/29/2024 |
2024.1334.722 | 78 | 11/29/2024 |
2024.1330.185 | 77 | 11/25/2024 |
2024.1328.505 | 83 | 11/23/2024 |
2024.1328.470 | 76 | 11/23/2024 |
2024.1328.452 | 75 | 11/23/2024 |
2024.1323.910 | 89 | 11/18/2024 |
2024.1323.653 | 74 | 11/18/2024 |
2024.1323.185 | 72 | 11/18/2024 |
2024.1323.49 | 69 | 11/18/2024 |
2024.1322.714 | 73 | 11/17/2024 |
2024.1320.653 | 81 | 11/15/2024 |
2024.1320.619 | 74 | 11/15/2024 |
2024.1320.601 | 68 | 11/15/2024 |
2024.1320.159 | 76 | 11/15/2024 |
2024.1320.98 | 76 | 11/15/2024 |
2024.1320.2 | 80 | 11/15/2024 |
2024.1319.1431 | 77 | 11/15/2024 |
2024.1319.1335 | 80 | 11/14/2024 |
2024.1317.631 | 81 | 11/12/2024 |
2024.1316.330 | 87 | 11/11/2024 |
2024.1311.540 | 76 | 11/6/2024 |
2024.1306.225 | 78 | 11/1/2024 |
2024.1306.199 | 83 | 11/1/2024 |
2024.1306.171 | 69 | 11/1/2024 |
2024.1306.117 | 84 | 11/1/2024 |
2024.1305.506 | 71 | 10/31/2024 |
2024.1305.502 | 71 | 10/31/2024 |
2024.1305.465 | 72 | 10/31/2024 |
2024.1305.442 | 72 | 10/31/2024 |
2024.1305.399 | 73 | 10/31/2024 |
2024.1299.1070 | 81 | 10/25/2024 |
2024.1289.621 | 82 | 10/15/2024 |
2024.1289.338 | 82 | 10/15/2024 |
2024.1277.836 | 83 | 10/5/2024 |
2024.1277.711 | 84 | 10/3/2024 |
2024.1277.695 | 82 | 10/3/2024 |
2024.1270.409 | 92 | 9/26/2024 |
2024.1269.621 | 95 | 9/25/2024 |
2024.1269.619 | 84 | 9/25/2024 |
2024.1269.605 | 94 | 9/25/2024 |
2024.1242.282 | 102 | 8/29/2024 |
2024.1242.261 | 98 | 8/29/2024 |
2024.1242.233 | 102 | 8/29/2024 |
2024.1238.829 | 115 | 8/25/2024 |
2024.1238.327 | 119 | 8/25/2024 |
2024.1225.297 | 107 | 8/12/2024 |
2024.1205.140 | 106 | 7/23/2024 |
2024.1204.110 | 102 | 7/22/2024 |
2024.1204.62 | 98 | 7/22/2024 |
2024.1190.491 | 107 | 7/8/2024 |
2024.1177.305 | 103 | 6/25/2024 |
2024.1168.700 | 111 | 6/16/2024 |
2024.1168.649 | 110 | 6/16/2024 |
2024.1168.535 | 116 | 6/16/2024 |
2024.1167.643 | 108 | 6/15/2024 |
2024.1166.989 | 102 | 6/14/2024 |
2024.1166.818 | 106 | 6/14/2024 |
2024.1166.535 | 110 | 6/14/2024 |
2024.1166.422 | 98 | 6/14/2024 |
2024.1166.362 | 105 | 6/14/2024 |
2024.1166.360 | 104 | 6/14/2024 |
2024.1165.1212 | 103 | 6/13/2024 |
2024.1165.1160 | 104 | 6/13/2024 |
2024.1165.1019 | 105 | 6/13/2024 |
2024.1165.978 | 101 | 6/13/2024 |
2024.1165.920 | 101 | 6/13/2024 |
2024.1165.908 | 103 | 6/13/2024 |
2024.1165.873 | 102 | 6/13/2024 |
2024.1165.830 | 102 | 6/13/2024 |
2024.1165.806 | 104 | 6/13/2024 |
2024.1165.794 | 103 | 6/13/2024 |
2024.1165.792 | 105 | 6/13/2024 |
2024.1165.761 | 103 | 6/13/2024 |
2024.1165.743 | 98 | 6/13/2024 |
2024.1165.735 | 103 | 6/13/2024 |
2024.1165.713 | 105 | 6/13/2024 |
2024.1165.640 | 97 | 6/13/2024 |
2024.1165.574 | 103 | 6/13/2024 |
2024.1165.466 | 102 | 6/13/2024 |
2024.1165.417 | 101 | 6/13/2024 |
2024.1165.392 | 99 | 6/13/2024 |
2024.1164.341 | 90 | 6/12/2024 |
2024.1155.941 | 114 | 6/3/2024 |
2024.1140.783 | 116 | 5/19/2024 |
2024.1140.692 | 104 | 5/19/2024 |
2024.1140.673 | 109 | 5/19/2024 |
2024.1129.506 | 103 | 5/8/2024 |
2024.1128.831 | 121 | 5/7/2024 |
2024.1124.465 | 85 | 5/3/2024 |
2024.1123.1162 | 76 | 5/2/2024 |
2024.1123.1117 | 76 | 5/2/2024 |
2024.1123.1050 | 69 | 5/2/2024 |
2024.1122.352 | 105 | 5/1/2024 |
2024.1122.233 | 104 | 5/1/2024 |
2024.1115.1101 | 113 | 4/24/2024 |
2024.1115.1048 | 97 | 4/24/2024 |
2024.1115.932 | 97 | 4/24/2024 |
2024.1115.858 | 108 | 4/24/2024 |
2024.1115.665 | 109 | 4/24/2024 |
2024.1115.648 | 101 | 4/24/2024 |
2024.1115.591 | 108 | 4/24/2024 |
2024.1101.390 | 105 | 4/10/2024 |
2024.1100.741 | 109 | 4/9/2024 |
2024.1099.526 | 115 | 4/8/2024 |
2024.1099.466 | 108 | 4/8/2024 |
2024.1098.573 | 114 | 4/7/2024 |
2024.1095.977 | 116 | 4/4/2024 |
2024.1095.847 | 109 | 4/4/2024 |
2024.1095.604 | 110 | 4/4/2024 |
2024.1095.485 | 127 | 4/4/2024 |
2024.1094.930 | 109 | 4/3/2024 |
2024.1094.910 | 121 | 4/3/2024 |
2024.1094.387 | 121 | 4/3/2024 |
2024.1094.313 | 109 | 4/3/2024 |
2024.1093.520 | 109 | 4/2/2024 |
2024.1092.240 | 109 | 4/1/2024 |
2024.1089.1425 | 126 | 3/29/2024 |
2024.1089.983 | 112 | 3/29/2024 |
2024.1089.942 | 101 | 3/29/2024 |
2024.1089.2 | 117 | 3/29/2024 |
2024.1088.1343 | 112 | 3/28/2024 |
2024.1088.1139 | 113 | 3/28/2024 |
2024.1088.875 | 87 | 3/28/2024 |
2024.1088.700 | 101 | 3/28/2024 |
2024.1088.603 | 115 | 3/28/2024 |
2024.1088.590 | 118 | 3/28/2024 |
2024.1088.575 | 113 | 3/28/2024 |
2024.1088.561 | 112 | 3/28/2024 |
2024.1088.506 | 116 | 3/28/2024 |
2024.1088.18 | 118 | 3/28/2024 |
2024.1087.1181 | 125 | 3/27/2024 |
2024.1087.1173 | 116 | 3/27/2024 |
2024.1087.1143 | 123 | 3/27/2024 |
2024.1087.1108 | 103 | 3/27/2024 |
2024.1087.950 | 113 | 3/27/2024 |
2024.1087.939 | 100 | 3/27/2024 |
2024.1087.252 | 118 | 3/27/2024 |
2024.1070.736 | 117 | 3/10/2024 |
2024.1049.1437 | 120 | 2/19/2024 |
2024.1036.571 | 125 | 2/5/2024 |
2024.1036.508 | 111 | 2/5/2024 |
2024.1032.1204 | 113 | 2/1/2024 |
2024.1032.1133 | 108 | 2/1/2024 |
2024.1032.1122 | 130 | 2/1/2024 |
2024.1032.1071 | 107 | 2/1/2024 |
2024.1032.940 | 103 | 2/1/2024 |
2024.1031.912 | 105 | 1/31/2024 |
2024.1031.893 | 106 | 1/31/2024 |
2024.1031.851 | 95 | 1/31/2024 |
2024.1031.829 | 102 | 1/31/2024 |
2024.1031.386 | 99 | 1/31/2024 |
2024.1031.318 | 121 | 1/31/2024 |
2024.1017.460 | 127 | 1/17/2024 |
2024.1017.420 | 105 | 1/17/2024 |
2024.1017.163 | 110 | 1/17/2024 |
2024.1016.414 | 563 | 1/16/2024 |
2024.1012.167 | 120 | 1/12/2024 |
2024.1011.406 | 119 | 1/11/2024 |
2024.1010.315 | 134 | 1/10/2024 |
2023.1358.326 | 165 | 12/24/2023 |
2023.1351.473 | 125 | 12/17/2023 |
2023.1351.445 | 128 | 12/17/2023 |
2023.1351.432 | 129 | 12/17/2023 |
2023.1350.478 | 128 | 12/16/2023 |
2023.1344.600 | 142 | 12/10/2023 |
2023.1344.529 | 147 | 12/10/2023 |
2023.1339.582 | 140 | 12/5/2023 |
2023.1338.1072 | 149 | 12/4/2023 |
2023.1336.844 | 140 | 12/2/2023 |
2023.1335.378 | 143 | 12/1/2023 |
2023.1335.236 | 136 | 12/1/2023 |
2023.1335.203 | 122 | 12/1/2023 |