Thursday, February 27, 2014

Use extension methods for enum

Create below custom attribute class, which will be used  for enum.

public class EnumDescAttribute : Attribute
    {

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="EnumDescAttribute"/> class.
        /// </summary>
        /// <param name="description">The description.</param>
        public EnumDescAttribute(string description)
        {
            Description = description;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public string Description { get; set; }

        #endregion
    }

Sample Enum:

public enum QualifierType
    {
        [EnumDescAttribute("QQ")]
        SubAccount,
        [EnumDescAttribute("AA")]
        Account,
        [EnumDescAttribute("BB")]
        LegacyDivision,
        [EnumDescAttribute("CC")]
        DateRange
    }

Create below Extension class

public static class EnumExtensions
    {
        #region Public methods

        /// <summary>
        /// Gets the description attribute.
        /// </summary>
        /// <param name="en">The en.</param>
        /// <returns></returns>
        public static string GetAttributeValue(this Enum en)
        {
            string returnValue = en.ToString();
            Type type = en.GetType();

            //MemberInfo[] memInfo = type.GetMember(en.ToString());
            //if (memInfo != null && memInfo.Length > 0)
            //{
            //    object[] attributes = memInfo[0].GetCustomAttributes(typeof(EnumDescAttribute), false);

            //    if (attributes != null && attributes.Length > 0)
            //    {
            //        returnValue = ((EnumDescAttribute)attributes[0]).Description;
            //    }

            //}
            EnumDescAttribute attribute = GetAttribute<EnumDescAttribute>(en);
            if (attribute != null)
            {
                returnValue = attribute.Description;
            }
            return returnValue;
        }

        /// <summary>
        /// Converts to enum.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static T ConvertToEnum<T>(this string value)
        {
            T returnValue = default(T);

            returnValue = (T)Enum.Parse(typeof(T), value, true);

            return returnValue;
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Gets the attribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private static T GetAttribute<T>(this Enum value) where T : Attribute
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
         
            return type.GetField(name)
                        .GetCustomAttributes(false)
                        .OfType<T>()
                        .FirstOrDefault();
        }

        #endregion
    }


Get enum attribute value:

static void Main(string[] args)
        {
            Console.WriteLine(consts.Enumerations.QualifierType.Account.GetAttributeValue());
            Console.ReadLine();
         
            string value ="Account";
            Console.WriteLine(value.ConvertToEnum<consts.Enumerations.QualifierType>().ToString());
            Console.ReadLine();
        }

No comments:

Post a Comment