Enum Extensions

 Here are some extension methods for Enum:

 

Take this enum for example:

public enum MyEnum
{
    None = 0,
    A = 1,
    B = 2,
    C = 4,
}

 

1. Parse:

/// <summary>
/// Converts the string representation of the name or numeric value of one or
/// more enumerated constants to an equivalent enumerated object
/// </summary>
/// <param name="value">A string containing the name or value to convert</param>
/// <returns>Enum whose value is represented by value</returns>
public static T Parse<T>(string value) where T : struct
{
    return Parse<T>(value, false);
}

/// <summary>
/// Converts the string representation of the name or numeric value of one or
/// more enumerated constants to an equivalent enumerated object
/// </summary>
/// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
/// <param name="value">A string containing the name or value to convert</param>
/// <returns>Enum whose value is represented by value</returns>
private static T Parse<T>(string value, bool ignoreCase) where T : struct
{
    return (T)Enum.Parse(typeof(T), value, ignoreCase);
}

 


 

Usage:

MyEnum e = EnumExtension.Parse<MyEnum>("A"); 

 

2. TryParse:

/// <summary>
/// Tries to convert the string representation of the name or numeric value of one or
/// more enumerated constants to an equivalent enumerated object.
/// If fails, set to default value
/// </summary>
/// <param name="value">A string containing the name or value to convert</param>
/// <returns>Enum whose value is represented by value</returns>
public static T TryParse<T>(string value) where T : struct
{
    return TryParse(value, default(T));
}

/// <summary>
/// Tries to convert the string representation of the name or numeric value of one or
/// more enumerated constants to an equivalent enumerated object.
/// If fails, set to @default
/// </summary>
/// <param name="value">A string containing the name or value to convert</param>
/// <param name="default">The default value to set if fail.</param>
/// <returns>Enum whose value is represented by value</returns>
public static T TryParse<T>(string value, T @default) where T : struct
{
    return TryParse<T>(value, @default, false);
}

/// <summary>
/// Tries to convert the string representation of the name or numeric value of one or
/// more enumerated constants to an equivalent enumerated object.
/// If fails, set to @default
/// </summary>
/// <param name="value">A string containing the name or value to convert</param>
/// <param name="default">The default value to set if fail.</param>
/// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
/// <returns>Enum whose value is represented by value</returns>
public static T TryParse<T>(string value, T @default, bool ignoreCase) where T : struct
{
    T retValue;
    try
    {
        retValue = Parse<T>(value, ignoreCase);
    }
    catch
    {
        retValue = @default;
    }
    return retValue;
}

Usage:

MyEnum e = EnumExtension.TryParse<MyEnum>("X", MyEnum.None);
// e = MyEnum.None

 

3. IsFlaged: Notice: uses reflection

public static bool IsFlaged<T>(this T enumObj) where T : struct
{
	return typeof(T).GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0;
}

 

Usage: 

bool isFlaged = e.IsFlaged();
// isFlaged = false

 

 

4. IsDefined:

public static bool IsDefined<T>(this T enumObj) where T : struct
{
	return Enum.IsDefined(typeof(T), enumObj);
}

 

Usage:

MyEnum e = (MyEnum)3;
bool isDefined = e.IsDefined();
//isDefined = false

 

Enjoy,

Ofir

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com