Class EnumHelper
- Namespace
- SunamoEnumsHelper
- Assembly
- SunamoEnumsHelper.dll
Provides helper methods for working with enums, including parsing, conversion, flag operations, and value retrieval.
public static class EnumHelper
- Inheritance
-
EnumHelper
- Inherited Members
Methods
EnumToString<T>(Type)
Converts an enum type to a dictionary mapping enum values to their lowercase string representations.
public static Dictionary<T, string> EnumToString<T>(Type type) where T : notnull
Parameters
typeTypeThe enum type to convert.
Returns
- Dictionary<T, string>
A dictionary mapping enum values to lowercase string names.
Type Parameters
TThe enum value type.
EnumToString<T>(T)
Converts enum flags to a comma-separated string representation.
public static string EnumToString<T>(T enumValue) where T : Enum
Parameters
enumValueTThe enum value to convert.
Returns
- string
A comma-separated string of enum flag names, excluding "Nope" values.
Type Parameters
TThe enum type.
GetAllCombinations<T>(bool)
Gets all enum combinations without zero and All values.
public static List<T> GetAllCombinations<T>(bool isSecondAll = true) where T : struct
Parameters
isSecondAllboolIf true, starts from index [1]. Otherwise from [0].
Returns
- List<T>
A list of all valid enum combinations.
Type Parameters
TThe enum type.
GetAllValues<T>(bool)
Gets all enum values without zero and All. If isSecondAll is true, will start from [1]. Otherwise from [0].
public static List<T> GetAllValues<T>(bool isSecondAll = true) where T : struct
Parameters
isSecondAllboolIf true, starts from index [1]. Otherwise from [0].
Returns
- List<T>
A list of all enum values excluding zero and All.
Type Parameters
TThe enum type.
GetEnumList<T>(List<T>, List<string>)
Parses a list of string values into enum values. Get values include zero and All. If parsing fails or list is null, returns the default list. Duplicates are added only once.
public static List<T> GetEnumList<T>(List<T> defaultValue, List<string> valuesToParse) where T : struct
Parameters
defaultValueList<T>The default list to return if parsing fails.
valuesToParseList<string>The list of string values to parse into enum values.
Returns
- List<T>
A list of parsed enum values, or the default list if parsing fails.
Type Parameters
TThe enum type.
GetFlags<T>(T)
Gets all flag names that are set in the specified enum value.
public static List<string> GetFlags<T>(T key) where T : Enum
Parameters
keyTThe enum value to get flags from.
Returns
Type Parameters
TThe enum type.
GetNames(Type)
Gets all enum value names for the specified enum type.
public static List<string> GetNames(Type type)
Parameters
typeTypeThe enum type to get names from.
Returns
GetValues<T>()
Gets all enum values excluding "Nope" and "None" values. Can be used only for int enums. For more control, use the overload with parameters.
public static List<T> GetValues<T>() where T : struct
Returns
- List<T>
A list of enum values excluding "Nope" and "None".
Type Parameters
TThe enum type.
GetValues<T>(bool, bool)
Gets all enum values with options to exclude Nope/None and Shared values.
public static List<T> GetValues<T>(bool isIncludingNope, bool isIncludingShared) where T : struct
Parameters
isIncludingNopeboolWhether to include "Nope" and "None" values.
isIncludingSharedboolWhether to include "Shared" or "Sha" values.
Returns
- List<T>
A list of enum values based on the specified inclusion criteria.
Type Parameters
TThe enum type.
ParseFromNumber<T, Number>(Number, T)
Parses a numeric value to an enum value. When trying to cast a number to an enum where this number doesn't exist, it casts and returns the number as string.
public static T ParseFromNumber<T, Number>(Number numericValue, T defaultValue) where T : struct
Parameters
numericValueNumberThe numeric value to convert to enum.
defaultValueTThe default enum value to return if conversion fails.
Returns
- T
The parsed enum value or the default value if conversion fails.
Type Parameters
TThe enum type to parse to.
NumberThe numeric type of the input value.
ParseNullable<T>(string, T?)
Parses a string to a nullable enum value, ignoring case.
public static T? ParseNullable<T>(string text, T? defaultValue) where T : struct
Parameters
textstringThe string to parse.
defaultValueT?The default nullable value to return if parsing fails.
Returns
- T?
The parsed enum value or the default value if parsing fails.
Type Parameters
TThe enum type to parse to.
Parse<T>(string, T, bool)
Parses a string to an enum value, ignoring case. Default value must be provided - default(T) cannot be returned because in comparing default(T) is always true for any value of T.
public static T Parse<T>(string text, T defaultValue, bool isReturningDefIfNull = false) where T : struct
Parameters
textstringThe string to parse.
defaultValueTThe default enum value to return if parsing fails.
isReturningDefIfNullboolIf true, returns the default value immediately without parsing.
Returns
- T
The parsed enum value or the default value if parsing fails.
Type Parameters
TThe enum type to parse to.