基于索引的枚举值检索 - C# [英] Retrieve value of Enum based on index - c#
问题描述
这是我的枚举:
公共枚举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,有没有办法让枚举值
我试过这样:
((DocumentTypes [])(Enum.GetValues(typeof运算(DocumentTypes))))[Convert.ToInt32(3)。的ToString()
返回'TYPE_4
字符串str =;
int值= 12
如果(Enum.IsDefined(typeof运算(DocumentTypes),值))
海峡=((DocumentTypes)值)的ToString();
,否则
海峡=无效值;
这使还将处理无效值试图使用,无需抛出的内部异常
您也可以替换DocumentTypes字符串,即
DocumentTypes DT = DocumentTypes 。无效; //创建一个无效的枚举
如果(Enum.IsDefined(typeof运算(DocumentTypes),值))
DT =(DocumentTypes)值;
而对于加分,这里是如何将自定义字符串添加到一个枚举(抄自< A HREF =http://stackoverflow.com/questions/630803/enum-with-strings/630817#630817>这个SO回答)
枚举DocumentType
{
[说明(我的文档类型1)]
类型1 = 1,
等...
}
然后某个位置添加extenstion方法
公共静态字符串GetDescription< T>(此对象enumerationValue)其中T:结构
{$ b $型b型= enumerationValue.GetType(); (!type.IsEnum)如果
抛出新的ArgumentException(EnumerationValue必须是枚举类型,enumerationValue);
//试图找到一个潜在的友好名称
//为枚举
的MemberInfo [] =的MemberInfo type.GetMember(enumerationValue.ToString())一个DescriptionAttribute;
如果(的MemberInfo = NULL&放大器;!&安培; memberInfo.Length大于0)
{
[对象] ATTRS =的MemberInfo [0] .GetCustomAttributes(typeof运算(DescriptionAttribute),FALSE);
如果(ATTRS = NULL&放大器;!&安培; attrs.Length大于0)
{
//拉出说明价值
回报率((DescriptionAttribute)的attrs [0])。说明;
}
}
//如果我们没有描述属性,只返回枚举
返回enumerationValue.ToString的的ToString();
}
然后你可以使用:
DocumentType DT = DocumentType.Type1;
字符串str = dt.GetDescription< DocumentType>();
这表示读取Description属性值。
< HR>
编辑 - 更新的代码
下面是一个简化版,需要知道类型的扩展方法的新版本的前手的枚举。
公共静态字符串GetDescription(枚举值)
{
变种类型= value.GetType();
VAR meminfo中= type.GetMember(value.ToString());
如果(memInfo.Length大于0)
{
VAR ATTRS = meminfo的[0] .GetCustomAttributes(typeof运算(DescriptionAttribute),FALSE);
如果(attrs.Length大于0)
回报率((DescriptionAttribute)ATTRS [0])说明。
}
返回value.ToString();
}
This is my enum:
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
}
If I want to obtain 'TYPE_8', if I only have 12, is there a way to get the enum value?
I tried this:
((DocumentTypes[])(Enum.GetValues(typeof(DocumentTypes))))[Convert.ToInt32("3")].ToString()
which returns a value of '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
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;
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();
}
Then you can use:
DocumentType dt = DocumentType.Type1;
string str = dt.GetDescription<DocumentType>();
Which will retrive the Description attribute value.
Edit - updated code
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屋!