这是我的枚举:
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,是否有办法获得枚举值?
我试过了:
((DocumentTypes[])(Enum.GetValues(typeof(DocumentTypes))))[Convert.ToInt32("3")].ToString()
返回值'TYPE_4'
答案 0 :(得分:34)
你可以直接施放它:
int value = 12;
DocumentTypes dt = (DocumentTypes)value;
答案 1 :(得分:23)
string str = "";
int value = 12;
if (Enum.IsDefined(typeof (DocumentTypes),value))
str = ((DocumentTypes) value).ToString();
else
str = "Invalid Value";
这个也会处理试图使用的无效值,而不会抛出内部异常
您也可以使用DocumentTypes替换字符串,即
DocumentTypes dt = DocumentTypes.Invalid; // Create an invalid enum
if (Enum.IsDefined(typeof (DocumentTypes),value))
dt = (DocumentTypes) value;
对于奖励点,以下是如何将自定义字符串添加到枚举(从this SO answer复制)
Enum DocumentType
{
[Description("My Document Type 1")]
Type1 = 1,
etc...
}
然后在某处添加扩展方法
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>();
将检索Description属性值。
这是一个新版本的扩展方法,不需要事先知道Enum的类型。
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();
}
答案 2 :(得分:6)
首先转换为你的枚举类型并调用ToString():
string str = ((DocumentTypes)12).ToString();
答案 3 :(得分:0)
试试这个:
public enum EnumTest { EnumOne, EnumTwo, EnumThree, Unknown }; public class EnumTestingClass{ [STAThread] static void Main() { EnumTest tstEnum = EnumTest.Unknown; object objTestEnum; objTestEnum = Enum.Parse(tstEnum.GetType(), "EnumThree"); if (objTestEnum is EnumTest) { EnumTest newTestEnum = (EnumTest)objTestEnum; Console.WriteLine("newTestEnum = {0}", newTestEnum.ToString()); } } }
现在从示例代码中您将看到newTestEnum
将具有“EnumTest”等效字符串“EnumThree”的值。
希望这有帮助, 最好的祝福, 汤姆。
答案 4 :(得分:0)
public enum Projects
{
Hotels = 1,
Homes = 2,
Hotel_Home = 3
}
int projectId = rRoom.GetBy(x => x.RoomId == room.RoomId).FirstOrDefault().ProjectId.TryToInt32();
Projects Project = (Projects)projectId;