从Description属性获取枚举 [英] Get Enum from Description attribute

查看:632
本文介绍了从Description属性获取枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/3422407/finding-an-enum-value-by-its-description-attribute">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(动物));
 

解决方案

 公共静态类EnumEx
{
    公共静态牛逼GetValueFromDescription&LT; T&GT;(字符串描述)
    {
        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&LT;动物&GT;(大熊猫);
 

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");

这篇关于从Description属性获取枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆