从描述属性获取Enum
Description
Enum
:
enum Animal{ [Description("")] NotSet = 0, [Description("Giant Panda")] GiantPanda = 1, [Description("Lesser Spotted Anteater")] LesserSpottedAnteater = 2}public static string GetDescription(this Enum value){ FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description;}
string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"
Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
拉风的咖菲猫