Table of Contents

Class Enumerator

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

Provides extension methods and utilities for enum types.

public static class Enumerator
Inheritance
Enumerator
Inherited Members

Methods

Contains(Enum, Enum)

Determines whether the enum value contains the specified flag. (Obsolete: Use Enum.HasFlag method.)

[Obsolete("Use Enum.HasFlag method.")]
public static bool Contains(this Enum enumSource, Enum enumPart)

Parameters

enumSource Enum

The source enum value.

enumPart Enum

The flag to check for.

Returns

bool

true if the flag is present; otherwise, false.

Contains<T>(T, T)

Determines whether the specified enum value contains the given flag(s). (Obsolete: Use HasFlags method.)

[Obsolete("Use HasFlags method.")]
public static bool Contains<T>(T enumSource, T enumPart)

Parameters

enumSource T

The source enum value.

enumPart T

The flag(s) to check for.

Returns

bool

true if the source contains the flag(s); otherwise, false.

Type Parameters

T

The enum type.

ExcludeNonBrowsable<T>(IEnumerable<T>)

Excludes non-browsable enum values from the collection.

public static IEnumerable<T> ExcludeNonBrowsable<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

The collection of enum values.

Returns

IEnumerable<T>

An enumerable of browsable enum values.

Type Parameters

T

The enum type.

ExcludeObsolete<T>(IEnumerable<T>)

Excludes obsolete enum values from the provided collection.

public static IEnumerable<T> ExcludeObsolete<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

The collection of enum values.

Returns

IEnumerable<T>

An enumerable without obsolete enum values.

Type Parameters

T

The enum type.

GetAttributeOfType<TAttribute>(object)

Retrieves an attribute of type TAttribute from the enum field value.

public static TAttribute GetAttributeOfType<TAttribute>(this object enumVal) where TAttribute : Attribute

Parameters

enumVal object

The enum value.

Returns

TAttribute

The attribute instance if found; otherwise, null.

Type Parameters

TAttribute

The type of attribute to retrieve.

Exceptions

ArgumentNullException

Thrown when enumVal is null.

GetEnumBaseType(Type)

Gets the underlying type of the specified enum type.

public static Type GetEnumBaseType(this Type enumType)

Parameters

enumType Type

The enum type.

Returns

Type

The underlying numeric type for the enum.

GetEnumBaseType<T>()

Gets the underlying base type of the enum type T.

public static Type GetEnumBaseType<T>()

Returns

Type

The underlying numeric type for the enum.

Type Parameters

T

The enum type.

GetName(Enum)

Gets the name of the enum value.

public static string GetName(this Enum value)

Parameters

value Enum

The enum value.

Returns

string

The name of the enum value.

GetName<T>(T)

Gets the name of the specified enum value.

public static string GetName<T>(T value)

Parameters

value T

The enum value.

Returns

string

The name of the enum value.

Type Parameters

T

The enum type.

GetNames(Type)

Gets all names of the specified enum type.

public static IEnumerable<string> GetNames(this Type enumType)

Parameters

enumType Type

The enum type.

Returns

IEnumerable<string>

An enumerable of enum names.

GetNames<T>()

Gets all names of the enum type T as a collection of strings.

public static IEnumerable<string> GetNames<T>()

Returns

IEnumerable<string>

An enumerable of enum names.

Type Parameters

T

The enum type.

GetValues(Type)

Gets all enum values for the specified enum type as objects.

public static IEnumerable<object> GetValues(this Type enumType)

Parameters

enumType Type

The enum type.

Returns

IEnumerable<object>

An enumerable of enum values as objects.

GetValues<T>()

Gets all enum values of type T.

public static IEnumerable<T> GetValues<T>()

Returns

IEnumerable<T>

An enumerable of all enum values.

Type Parameters

T

The enum type.

HasFlags<T>(T, T)

Determines whether the specified enum value has the given flag(s).

public static bool HasFlags<T>(T enumSource, T enumPart)

Parameters

enumSource T

The source enum value.

enumPart T

The flag(s) to check for.

Returns

bool

true if the flag(s) are present; otherwise, false.

Type Parameters

T

The enum type.

IsDefined<T>(T)

Determines whether the specified enum value is defined in its type.

public static bool IsDefined<T>(this T enumValue)

Parameters

enumValue T

The enum value.

Returns

bool

true if the value is defined; otherwise, false.

Type Parameters

T

The enum type.

IsEnumBrowsable(object)

Determines whether the enum value is marked as browsable.

public static bool IsEnumBrowsable(this object enumVal)

Parameters

enumVal object

The enum value.

Returns

bool

true if the enum value is browsable; otherwise, false.

IsFlags(Type)

Determines whether the specified enum type has the Flags attribute.

public static bool IsFlags(this Type enumType)

Parameters

enumType Type

The enum type.

Returns

bool

true if the Flags attribute is present; otherwise, false.

JoinMask<T>()

Joins all flags of type T into a single masked enum value.

public static T JoinMask<T>()

Returns

T

The joined masked enum value.

Type Parameters

T

The enum type.

JoinMask<T>(IEnumerable<T>)

Joins the provided collection of enum values into a single masked enum value.

public static T JoinMask<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

An enumerable of enum values.

Returns

T

The resulting joined masked enum value.

Type Parameters

T

The enum type.

Exceptions

ArgumentNullException

Thrown when values is null.

Remove<T>(Enum, T)

Removes the specified flag from the enum value.

public static T Remove<T>(this Enum enumSource, T enumPart)

Parameters

enumSource Enum

The source enum value.

enumPart T

The flag to remove.

Returns

T

The enum value after removal of the flag.

Type Parameters

T

The enum type.

Exceptions

ArgumentException

Thrown when the type of enumPart does not match enumSource.

Remove<T>(T, T)

Removes the specified flag(s) from the source enum value and returns the result.

public static T Remove<T>(T enumSource, T enumPart)

Parameters

enumSource T

The source enum value.

enumPart T

The flag(s) to remove.

Returns

T

The enum value after removal of the specified flag(s).

Type Parameters

T

The enum type.

SplitMask2(object)

Splits the masked enum value into its constituent flag values and returns them as objects.

public static IEnumerable<object> SplitMask2(this object maskedValue)

Parameters

maskedValue object

The masked enum value.

Returns

IEnumerable<object>

An enumerable of individual flag values as objects.

Exceptions

ArgumentNullException

Thrown when maskedValue is null.

SplitMask<T>(T)

Splits the masked enum value into its constituent flag values of type T.

public static IEnumerable<T> SplitMask<T>(this T maskedValue)

Parameters

maskedValue T

The masked enum value.

Returns

IEnumerable<T>

An enumerable of individual flag values of type T.

Type Parameters

T

The enum type.

TryParse<T>(string, out T, bool)

Attempts to parse the string to an enum value of type T.

public static bool TryParse<T>(this string str, out T value, bool ignoreCase = true) where T : struct

Parameters

str string

The string representation of the enum value.

value T

When this method returns, contains the enum value equivalent to the string, if the parse succeeded.

ignoreCase bool

if set to true ignores case during parsing.

Returns

bool

true if the parse was successful; otherwise, false.

Type Parameters

T

The enum type.