从Description属性获取枚举属性、Description

2023-09-02 10:15:44 作者:相关个性符号推荐>>

可能重复:   Finding其描述属性的枚举值

我有得到描述一个通用的扩展方法从属性的枚举

 枚举动物
{
    [描述()]
    NotSet,要= 0,

    [描述(熊猫)
    GiantPanda = 1,

    [描述(小斑食蚁兽)
    LesserSpottedAnteater = 2
}

公共静态字符串GetDescription(枚举值)
{
    字段信息字段= value.GetType()getfield命令(value.ToString())。

    DescriptionAttribute属性
            = Attribute.GetCustomAttribute(场的typeof(DescriptionAttribute))
                作为DescriptionAttribute;

    返回属性== NULL? value.ToString():attribute.Description;
}
 

所以我可以做......

 字符串myAnimal = Animal.GiantPanda.GetDescription(); // =大熊猫
 

现在,我试图找出同等功能的其他方向,类似...

 动物=(动物)Enum.GetValueFromDescription(大熊猫的typeof(动物));
 
JS 枚举对象中的属性

解决方案

 公共静态类EnumEx
{
    公共静态牛逼GetValueFromDescription< T>(字符串描述)
    {
        VAR类型= ty​​peof运算(T);
        (!type.IsEnum),如果抛出新的InvalidOperationException异常();
        的foreach(VAR在type.GetFields)场()
        {
            var属性= Attribute.GetCustomAttribute(场,
                typeof运算(DescriptionAttribute))作为DescriptionAttribute;
            如果(属性!= NULL)
            {
                如果(attribute.Description ==介绍)
                    返程(T)field.GetValue(空);
            }
            其他
            {
                如果(field.Name ==介绍)
                    返程(T)field.GetValue(空);
            }
        }
        抛出新的ArgumentException(未找到,说明);
        //或返回默认值(T);
    }
}
 

用法:

 变种熊猫= EnumEx.GetValueFromDescription<动物>(大熊猫);
 

Possible Duplicate: Finding an enum value by its Description Attribute

I have a generic extension method which gets the Description attribute from an 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;
}

so I can do...

string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"

now, I'm trying to work out the equivalent function in the other direction, something like...

Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));

解决方案

public static class EnumEx
{
    public static T GetValueFromDescription<T>(string description)
    {
        var type = typeof(T);
        if(!type.IsEnum) throw new InvalidOperationException();
        foreach(var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;
            if(attribute != null)
            {
                if(attribute.Description == description)
                    return (T)field.GetValue(null);
            }
            else
            {
                if(field.Name == description)
                    return (T)field.GetValue(null);
            }
        }
        throw new ArgumentException("Not found.", "description");
        // or return default(T);
    }
}

Usage:

var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");