/ Published in: C#
By default, enum values cannot have spaces in them (i.e. "Created Date" can only be "CreatedDate"). With this method, we can change that!
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System.ComponentModel; public class MyClass { /// <summary> /// Returns the description attribute of an Enum if available, othewise returns /// the toString() of the value passed in. /// /// Useful for Enums with spaces in them. /// </summary> /// <param name="value">the enum</param> /// <returns>the description string of the enum</returns> public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return value.ToString(); } } public enum Types { [Description("Created Date")] CreatedDate, Description, Test }