EnumHelper Class
Namespace: PTS
Assembly: PTSframework.dll
Provides a set of static methods for enum type.
Syntax
public static class EnumHelper
Inheritance
Methods
CastByName<TEnum>(Enum)
Converts enum value to another type of enum based on field name.
Declaration
public static TEnum CastByName<TEnum>(this Enum value)
where TEnum : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | Enum value to convert. |
Returns
| Type | Description |
|---|---|
| TEnum | Enum value matching the original value name. |
Type Parameters
| Name | Description |
|---|---|
| TEnum | Type of enum to convert the original enum value to. |
EnumName(Enum)
Retrieves the name of the constant in given enum type that has the specified value.
Declaration
public static string EnumName(this Enum value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.String | A string containing the name of the enumerated constant in given |
EnumOptions<TEnum>(Boolean)
Retrieves array of values of the constants in given enum type.
Declaration
public static TEnum[] EnumOptions<TEnum>(bool skipDefault = false)
where TEnum : Enum
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | skipDefault | If set to |
Returns
| Type | Description |
|---|---|
| TEnum[] | An array that contains the values of the constants in given enum type. |
Type Parameters
| Name | Description |
|---|---|
| TEnum | Type of |
EnumTypeCheck<TEnum>(Boolean)
Checks whether given type is of enum.
Declaration
public static void EnumTypeCheck<TEnum>(bool withFlags)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Boolean | withFlags | If set to |
Type Parameters
| Name | Description |
|---|---|
| TEnum | Type of element to check. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | Thrown if given element type is not |
FullyQualifiedName(Enum)
Retrieves the namespace, type and name of the constant in given enum type that has the specified value.
Declaration
public static string FullyQualifiedName(this Enum value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.String | A string containing the namespace, type and name of the enumerated constant in given |
Remarks
Example of returned value: PTS.Module.Users.Gender.Male
GetAttributes<TAttribute>(Enum)
Retrieves an array of the custom attributes applied to a specific enum value.
Declaration
public static List<TAttribute> GetAttributes<TAttribute>(this Enum value)
where TAttribute : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<TAttribute> | An array that contains the custom attributes of type type applied to |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | Type of the custom attribute to search for. |
GetValueDescriptor(Enum)
Converts the enum value to a pair of a enum integer value as key and enum field name as value.
Declaration
public static KeyValuePair<int, string> GetValueDescriptor(this Enum value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.KeyValuePair<System.Int32, System.String> | Pair of a enum integer value as key and enum field name as value. |
IsEquivalent(Enum, Enum)
Compares two enum options based on their integer values.
Declaration
public static bool IsEquivalent(this Enum value, Enum other)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The primary value of a particular enumerated constant in given |
| System.Enum | other | The other value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.Boolean | If the integer values in both |
ParseEnumOptionName(String)
Extracts the name of enum constant from fully qualified name string representation.
Declaration
public static string ParseEnumOptionName(string input)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | input | String value to extract the |
Returns
| Type | Description |
|---|---|
| System.String | String represantion of the |
Remarks
Parsed example: GenderEnum.Male parses into Male.
ParseEnumTypeName(String)
Extracts the enum type name from fully qualified name string representation.
Declaration
public static string ParseEnumTypeName(string input)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | input | String value to extract the |
Returns
| Type | Description |
|---|---|
| System.String | String represantion of the |
PrettyName(Enum)
Retrieves human readable name representation of the constant in given enum type that has the specified value.
The method inserts a white space character before each capital letter preceeded by a lower case character.
Declaration
public static string PrettyName(this Enum value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.String | A string containing the namespace, type and name of the enumerated constant in given |
Remarks
Example of conversion: Enum option NeedsEmailVerification produces Needs Email Verification.
SearchInLabels<TEnum>(String, Func<TEnum, String>)
Searches the search word in enum name for all enum values.
Declaration
public static List<TEnum> SearchInLabels<TEnum>(string searchWord, Func<TEnum, string> labelSelector = null)
where TEnum : Enum
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | searchWord | The search word. |
| System.Func<TEnum, System.String> | labelSelector | The label selector. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<TEnum> | System.Collections.Generic.List<T> with matching enum values |
Type Parameters
| Name | Description |
|---|---|
| TEnum | The type of the enum. |
ToEnum(Object, Type)
Converts the specified object to an enumeration member.
Declaration
public static object ToEnum(object value, Type enumType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The value convert to an enumeration member. |
| System.Type | enumType | The enumeration type to return. |
Returns
| Type | Description |
|---|---|
| System.Object | An enumeration object with equivalent value. |
ToEnum(Object, Type, Type)
Converts the specified object to an enumeration member.
Declaration
public static object ToEnum(object value, Type enumType, Type inputType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The value convert to an enumeration member. |
| System.Type | enumType | The enumeration type to return. |
| System.Type | inputType | The type of input object. |
Returns
| Type | Description |
|---|---|
| System.Object | An enumeration object with equivalent value. |
ToEnum<TEnum>(Object)
Converts the specified object to an enumeration member.
Declaration
public static TEnum ToEnum<TEnum>(object value)
where TEnum : Enum
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The value convert to an enumeration member. |
Returns
| Type | Description |
|---|---|
| TEnum | An enumeration object with equivalent value. |
Type Parameters
| Name | Description |
|---|---|
| TEnum | The enumeration type to return. |
ToEnum<TEnum>(String, Boolean)
Converts the specified string to an enumeration member.
Declaration
public static TEnum ToEnum<TEnum>(string value, bool ignoreCase = true)
where TEnum : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | value | The value convert to an enumeration member. |
| System.Boolean | ignoreCase | If set to |
Returns
| Type | Description |
|---|---|
| TEnum | An enumeration object with equivalent value if present, othewise default value of given |
Type Parameters
| Name | Description |
|---|---|
| TEnum | The enumeration type to return. |
ToInt32(Enum)
Converts the enum value to a 32-bit signed integer.
Declaration
public static int ToInt32(this Enum value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.Int32 | A 32-bit signed integer equivalent to |
ToNullableEnum<TEnum>(String, Boolean)
Converts the specified string to a nullable enumeration member.
Declaration
public static TEnum? ToNullableEnum<TEnum>(string value, bool ignoreCase = true)
where TEnum : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | value | The value convert to an enumeration member. |
| System.Boolean | ignoreCase | If set to |
Returns
| Type | Description |
|---|---|
| System.Nullable<TEnum> | A nullable enumeration object with equivalent value if present, othewise |
Type Parameters
| Name | Description |
|---|---|
| TEnum | The enumeration type to return as nullable. |
ToUInt32(Enum)
Converts the enum value to a 32-bit unsigned integer.
Declaration
public static uint ToUInt32(this Enum value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Enum | value | The value of a particular enumerated constant in given |
Returns
| Type | Description |
|---|---|
| System.UInt32 | A 32-bit unsigned integer equivalent to |