K výčtovému typu enum nelze konvenčními metodami udělat vlastní textovou reprezentaci, není jak overridovat metodu ToString(). Pokud chceme každé hodnotě přiřadit pouze jedinou „user-friendly“ textovou reprezentaci, můžeme využít atributu DescriptionAttribute:
public enum StavZakazky { [Description("Nedefinován")] Nedefinovan, [Description("Vytištěno")] TiskHotovo } public static class EnumExt { public static string GetDescription(Type enumType, object hodnota) { string strRet = "<na>"; try { System.Reflection.FieldInfo objInfo = enumType.GetField(Enum.GetName(enumType, hodnota)); System.ComponentModel.DescriptionAttribute objDescription = (System.ComponentModel.DescriptionAttribute)objInfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true)[0]; strRet = objDescription.Description; } catch(Exception) { // chybí description } return strRet; } }
Interní: Implementováno jako Havit.EnumExt.GetDescription().