Return to Snippet

Revision: 30729
at August 20, 2010 09:13 by samdanae


Initial Code
public enum PaymentTypes
    {
        [Description("Mooring Licence Fee")]
        MooringLicenceFee = 1, 
        Contact = 2,
        Vessel = 3,
        Enquiry = 4,
        [Description("Inspection Entry")]
        InspectionEntry = 5,
        Lease = 6,
        [Description("Licence Transfer")]
        LicenceTransfer = 7,
        Other = 8
    }

public static class Extensions
    {
        public static string GetDescription(this PaymentTypes value)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr == null)
                    {
                        return name;
                    }
                    else
                    {
                        return attr.Description;
                    }
                }
            }
            return null;

        }
    }

Initial URL


Initial Description
Description attribute retrieval using an extension method.

Initial Title
Get enum description

Initial Tags


Initial Language
C#