public enum MenuOptions { [Description("Park Vehicle")] ParkVehicle = 1, [Description("Exit Vehicle")] ExitVehicle = 2, [Description("Check Slot is Empty")] CheckSlotIsEmpty = 3, [Description("Parked Vehicles List")] ParkedVehiclesList = 4, [Description("Exit")] Exit = 5 }
Code to get the Enum Description:
public String GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes (typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); }
The usage of enum description as follows
public static void GetNavigation() { foreach (int i in Enum.GetValues(typeof(MenuOptions))) Console.WriteLine("{0}. {1}", i,GetEnumDescription((Enum)Enum.Parse(typeof(MenuOptions),Enum.GetName(typeof(MenuOptions), i)))); }
No comments:
Post a Comment