/ Published in: C#

Description attribute retrieval using an extension method.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } }
Comments
