Class EnumHelpers
Provides helper functions for enum types.
public static class EnumHelpers
- Inheritance
-
EnumHelpers
- Inherited Members
Methods
GetEntryCount(Type)
Gets the number of entries defined in the specified enum type.
public static int GetEntryCount(Type type)
Parameters
typeTypeThe enum type whose entry count to get.
Returns
- int
The number of entries defined in the enum type.
Remarks
This function has a O(n) time complexity if the provided type has not been evaluated with either this, or the GetEntryCount<T>() function, otherwise it has a O(1) complexity. Thus, prefer calling the GetEntryCount<T>() function wherever possible.
Exceptions
- ArgumentException
The provided type is null, or not an enum type.
GetEntryCount<T>()
Gets the number of entries defined in the specified enum type.
public static int GetEntryCount<T>() where T : unmanaged, Enum
Returns
- int
The number of entries defined in the enum type.
Type Parameters
TThe enum type whose entry count to get.
Remarks
This function has a O(1) time complexity, contrast to GetEntryCount(Type), since the count is stored as a runtime constant. Thus, prefer calling this function wherever possible.
IsDefined<TEnum, TUnderlying>(TUnderlying)
Determines whether a value is defined in the enum TEnum.
public static bool IsDefined<TEnum, TUnderlying>(TUnderlying value) where TEnum : unmanaged, Enum where TUnderlying : unmanaged
Parameters
valueTUnderlyingThe integral value to determine whether it's defined in an enum.
Returns
Type Parameters
TEnumThe type of the enum.
TUnderlyingThe underlying type of the enum, which is the type of the value instances.
Remarks
The function is based on IsDefined<TEnum>(TEnum).
IsEnumOfType<TEnum, TUnderlying>()
Determines whether TEnum's underlying value type
is TUnderlying.
public static bool IsEnumOfType<TEnum, TUnderlying>() where TEnum : unmanaged, Enum where TUnderlying : unmanaged
Returns
Type Parameters
TEnumThe type of the enum.
TUnderlyingThe underlying type of the enum, which is the type of the value instances.
RegisterEntryCounts(IEnumerable<Type>)
Registers the entry counts for the enum types from the given types.
public static void RegisterEntryCounts(IEnumerable<Type> types)
Parameters
typesIEnumerable<Type>The collection of types, from which the enum types' entry counts will be registered.
Remarks
Ideally, this should only be called once per type in the collection.
RegisterEntryCounts(Assembly)
Discovers all enum types from the specified assembly, and statically registers their entry counts.
public static void RegisterEntryCounts(Assembly assembly)
Parameters
assemblyAssemblyThe assembly whose enum types to discover.
Remarks
Ideally, this should only be called once per assembly.
RegisterEntryCountsGlobally()
Discovers all enum types from all the loaded assemblies, using Current, and statically registers their entry counts.
public static void RegisterEntryCountsGlobally()
Remarks
Ideally, this should only be called once per execution.