0

Possible Duplicate:
Enumeration extension methods

I have a situation where i would like to add an extension to a couple of my Enumerators for quick extraction of information. already wrote a static method to do this as a helper method but was wondering if it was possible to also short it as an extension?

signature as it is now, again not sure its possible. Just pushing my knowledge threshold and boundaries ;)

public static string EnumString( this Type par , object val ) {
    return Enum.GetName( typeof( par ) , val );
}
Community
  • 1
  • 1
GoldBishop
  • 2,820
  • 4
  • 47
  • 82

1 Answers1

0

Resulting Method as described by JordonKaye

Extension:
/// <summary>
/// Returns the string value of the Enumerator
/// </summary>
/// <param name="par">Type object, Should be an Enumerator Type</param>
/// <param name="val">Object type, Should be the value of the Enumerator to return as string</param>
/// <returns>String value, the String value form of the Enumerator item.
/// If the passed object value (@val) is NOT valid, Returns NULL
/// If passed object value (@val) IS valid, Returns string value of @val</returns>
public static string EnumString( this Enum par , object val ) {
    if( Enum.IsDefined( par.GetType() , val ) ) {
        return Enum.GetName( par.GetType() , val );
    } else {
        return null;
    }
}
Shared method, Helper line:
public static string EnumString( Enum par , object val ) {
    if( Enum.IsDefined( par.GetType() , val ) ) {
        return Enum.GetName( par.GetType() , val );
    } else {
        return null;
    }
}
GoldBishop
  • 2,820
  • 4
  • 47
  • 82