从描述属性获取Enum
DescriptionEnum:
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));
MM们
拉风的咖菲猫
随时随地看视频慕课网APP