namespace Zeta.EnterpriseLibrary.Data { using System; [AttributeUsage( AttributeTargets.All, Inherited = false, AllowMultiple = true )] public sealed class EnumModeHintAttribute : Attribute { #region Public methods. // ------------------------------------------------------------------ public EnumModeHintAttribute( string textValue ) { _textValue = textValue; } public static string GetTextValue( Enum value ) { var fi = value.GetType().GetField( value.ToString() ); var attributes = (EnumModeHintAttribute[])fi.GetCustomAttributes( typeof( EnumModeHintAttribute ), false ); if ( attributes != null && attributes.Length > 0 ) { return attributes[0].TextValue; } else { return null; } } private static string getTextValue( object value ) { var fi = value.GetType().GetField( value.ToString() ); var attributes = (EnumModeHintAttribute[])fi.GetCustomAttributes( typeof( EnumModeHintAttribute ), false ); if ( attributes != null && attributes.Length > 0 ) { return attributes[0].TextValue; } else { return null; } } public static T GetEnum<T>( string textValue ) { if ( string.IsNullOrEmpty( textValue ) ) { return default( T ); } else { foreach ( T value in Enum.GetValues( typeof( T ) ) ) { var v = getTextValue( value ); if ( !string.IsNullOrEmpty( v ) && string.Compare( v, textValue, true ) == 0 ) { return value; } } return default( T ); } } // ------------------------------------------------------------------ #endregion #region Public properties. // ------------------------------------------------------------------ public string TextValue { get { return _textValue; } } // ------------------------------------------------------------------ #endregion #region Private variables. // ------------------------------------------------------------------ private readonly string _textValue; // ------------------------------------------------------------------ #endregion } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)