PvWayEnumConvNc8 8.0.0
dotnet add package PvWayEnumConvNc8 --version 8.0.0
NuGet\Install-Package PvWayEnumConvNc8 -Version 8.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="PvWayEnumConvNc8" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PvWayEnumConvNc8" Version="8.0.0" />
<PackageReference Include="PvWayEnumConvNc8" />
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 PvWayEnumConvNc8 --version 8.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PvWayEnumConvNc8, 8.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 PvWayEnumConvNc8@8.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=PvWayEnumConvNc8&version=8.0.0
#tool nuget:?package=PvWayEnumConvNc8&version=8.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EnumConvert
Introduction
Provides utility static methods for converting between enum values and their associated codes. This class supports operations such as retrieving the primary code for an enum value, finding the enum value corresponding to a given code, and handling descriptions with multiple codes separated by commas.
Usage
using PvWayEnumConvNc8;
Console.WriteLine("Integration testing for EnumConvert");
Console.WriteLine("===================================");
// Getting the primary code representing the Severity Fatal
var codeForFatal = Severity.Fatal.GetCode();
Console.WriteLine($"The primary code for Severity.Fatal is '{codeForFatal}'.");
// Getting the severity value corresponding to the code 'V'
var severity = EnumConvert.GetValue<Severity>("V");
Console.WriteLine($"The Severity corresponding to code 'V' is '{severity}'");
// Getting the default value when the code cannot be found
var defaultValue = Severity.Info;
var severityOrDefault = EnumConvert.GetValue("X", defaultValue);
Console.WriteLine($"The Severity corresponding to code 'X' is '{severityOrDefault}'");
/* Console Output
Integration testing for EnumConvert
===================================
The primary code for Severity.Fatal is 'F'.
The Severity corresponding to code 'V' is 'Trace'
The Severity corresponding to code 'X' is 'Info'
*/
internal enum Severity {
[System.ComponentModel.Description("F,Fatal,C,Crit,Critical")]
Fatal,
[System.ComponentModel.Description("E,Err,Error")]
Error,
[System.ComponentModel.Description("W,Warn,Warinng")]
Warning,
[System.ComponentModel.Description("I,Info")]
Info,
[System.ComponentModel.Description("D,Debug")]
Debug,
[System.ComponentModel.Description("T,Trc,Trace,V,Vrb,Verbose")]
Trace,
}
Methods
GetCode
string GetCode(this Enum value)
/// <summary>
/// <p>Gets the code (first term) from the raw description of the specified Enum value.</p>
/// <p>The raw description can contain a list of ',' separated terms (codes) for a given enum value</p>
/// </summary>
/// <param name="value">The Enum value from which to retrieve the code.</param>
/// <returns>Returns the code (first term) extracted from the raw description of the Enum value if available; otherwise, throws an exception.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static string GetCode(this Enum value)
{
var rawDescription = GetRawDescription(value);
if (!rawDescription.Contains(',')) return rawDescription;
var codeList = rawDescription.Split(',', StringSplitOptions.TrimEntries);
return codeList[0];
}
GetValue
T GetValue<T>(string code) where T : Enum
/// <summary>
/// Returns the Enum value that corresponds to the specified code based on the default Equality matcher
/// </summary>
/// <param name="code">The code to search for within the Enum values descriptions.</param>
/// <returns>The Enum value associated with the given code if found; otherwise, throws an ArgumentOutOfRangeException.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static T GetValue<T>(string code) where T : Enum
{
return GetValue<T>(code, EqualityMatcher);
}
T GetValue<T>(string code, Func<string, string, bool> match)
/// <summary>
/// Retrieves the Enum value based on the provided code using the specified match function.
/// </summary>
/// <param name="code">The code to search for within the Enum values descriptions.</param>
/// <param name="match">The custom match function used to determine the comparison between the code and Enum descriptions.</param>
/// <returns>The Enum value associated with the given code if found; otherwise, throws an ArgumentOutOfRangeException.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static T GetValue<T>(
string code,
Func<string, string, bool> match) where T : Enum
{
if (string.IsNullOrEmpty(code))
throw new ArgumentOutOfRangeException(
nameof(code), code);
var found = TryFindValue<T>(code, match, out var value);
if (found) return value!;
throw new ArgumentOutOfRangeException(
nameof(code), code);
}
T GetValue<T>(string? code, T defaultValue)
/// <summary>
/// Returns the Enum value that corresponds to the specified code based on the default Equality matcher
/// </summary>
/// <param name="code">The code to search for within the Enum values descriptions.</param>
/// <param name="defaultValue">The default Enum value to return if no matching value is found.</param>
/// <returns>The Enum value that corresponds to the specified code if found; otherwise, the default value.</returns>
public static T GetValue<T>(
string? code,
T defaultValue) where T : Enum =>
GetValue(code, defaultValue, EqualityMatcher);
T GetValue<T>(string? code, T defaultValue, Func<string, string, bool> match)
/// <summary>
/// Returns the Enum value that corresponds to the specified code based on a custom match function.
/// </summary>
/// <param name="code">The code to search for within the Enum values descriptions.</param>
/// <param name="defaultValue">The default Enum value to return if no matching value is found.</param>
/// <param name="match">A custom match function that defines the comparison logic between the code and Enum descriptions.</param>
/// <returns>The Enum value that corresponds to the specified code if found; otherwise, the default value.</returns>
public static T GetValue<T>(
string? code,
T defaultValue,
Func<string, string, bool> match) where T : Enum
{
if (string.IsNullOrEmpty(code)) return defaultValue;
var found = TryFindValue<T>(code, match, out var value);
return found ? value! : defaultValue;
}
| 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. net9.0 was computed. 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 was computed. 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.
-
net8.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 |
|---|---|---|
| 8.0.0 | 213 | 3/28/2025 |
Initial