Add 'backend/' from commit '040cfd7a75423d4e6136e58a67b40579af4ee966'
git-subtree-dir: backend git-subtree-mainline:ab911955edgit-subtree-split:040cfd7a75
This commit is contained in:
34
backend/src/Web/Extensions/EnumExtensions.cs
Normal file
34
backend/src/Web/Extensions/EnumExtensions.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace Hutopy.Web.Extensions;
|
||||
|
||||
public static class EnumExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a string to the specified enum type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">The type of the enum to convert to. Must be an enum.</typeparam>
|
||||
/// <param name="value">The string value to convert.</param>
|
||||
/// <param name="ignoreCase">Specifies whether the string comparison should ignore case. Default is true.</param>
|
||||
/// <returns>
|
||||
/// The corresponding enum value if the conversion is successful; otherwise, null if the string
|
||||
/// cannot be converted to the specified enum type.
|
||||
/// </returns>
|
||||
public static TEnum? ToEnum<TEnum>(this string value, bool ignoreCase = true) where TEnum : struct
|
||||
{
|
||||
if (Enum.TryParse(value, ignoreCase, out TEnum result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an enum value to its string representation.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">The type of the enum.</typeparam>
|
||||
/// <param name="enumValue">The enum value to convert.</param>
|
||||
/// <returns>The string representation of the enum value.</returns>
|
||||
public static string FromEnum<TEnum>(this TEnum enumValue) where TEnum : struct, Enum
|
||||
{
|
||||
return enumValue.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user