基于索引检索枚举值 - c# [英] Retrieve value of Enum based on index - c#

查看:90
本文介绍了基于索引检索枚举值 - c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的枚举:

public enum DocumentTypes
    {
        [EnumMember]
        TYPE_1 = 1,
        [EnumMember]
        TYPE_2 = 2,
        [EnumMember]
        TYPE_3 = 3,
        [EnumMember]
        TYPE_4 = 4,
        [EnumMember]
        TYPE_5 = 5,
        [EnumMember]
        TYPE_6 = 6,
        [EnumMember]
        TYPE_7 = 7,
        [EnumMember]
        TYPE_8 = 12

    }

如果我想要获得TYPE_8,如果我只有12,有没有办法获取枚举值?

If I want to obtain 'TYPE_8', if I only have 12, is there a way to get the enum value?

我试过这个:

((DocumentTypes[])(Enum.GetValues(typeof(DocumentTypes))))[Convert.ToInt32("3")].ToString()

返回值TYPE_4

推荐答案

string str = "";
int value = 12
if (Enum.IsDefined(typeof (DocumentTypes),value))
     str =  ((DocumentTypes) value).ToString();
else
     str = "Invalid Value";

此给定还将处理尝试使用的无效值,而不会引发内部异常

This gives will also handle invalid values trying to be used, without the internal exception being thrown

您还可以用DocumentTypes替换字符串,即

You can also replace the string with DocumentTypes, ie

DocumentTypes dt = DocumentTypes.Invalid; // Create an invalid enum
if (Enum.IsDefined(typeof (DocumentTypes),value))
   dt = (DocumentTypes) value;

对于奖励点,以下是如何将自定义字符串添加到枚举(从< a href =https://stackoverflow.com/questions/630803/enum-with-strings/630817#630817>此SO答案)

And for the bonus point, here is how to add a custom string to an enum (copied from this SO answer)

Enum DocumentType
{ 
    [Description("My Document Type 1")]
    Type1 = 1,
    etc...
}

然后在某处添加一个扩展方法

Then add an extenstion method somewhere

public static string GetDescription<T>(this object enumerationValue) where T : struct
{
    Type type = enumerationValue.GetType();
    if (!type.IsEnum)
        throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");

    //Tries to find a DescriptionAttribute for a potential friendly name
    //for the enum
    MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
    if (memberInfo != null && memberInfo.Length > 0)
    {
        object[] attrs = memberInfo[0].GetCustomAttributes(typeof (DescriptionAttribute), false);

        if (attrs != null && attrs.Length > 0)
        {
            //Pull out the description value
            return ( (DescriptionAttribute) attrs[0] ).Description;
        }
    }
    //If we have no description attribute, just return the ToString of the enum
    return enumerationValue.ToString();
}

然后你可以使用:

DocumentType dt = DocumentType.Type1;
string str = dt.GetDescription<DocumentType>();

哪些将重新出现描述属性值。

Which will retrive the Description attribute value.

这是一个新版本的扩展方法,不需要知道类型的枚举。

Here is a new version of the extension method that does't need to know the type of the Enum before hand.

public static string GetDescription(this Enum value)
{
    var type = value.GetType();

    var memInfo = type.GetMember(value.ToString());

    if (memInfo.Length > 0)
    {
        var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attrs.Length > 0)
            return ((DescriptionAttribute)attrs[0]).Description;
    }

    return value.ToString();
}

这篇关于基于索引检索枚举值 - c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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