C# 枚举设置为字符串并在需要时获取刺痛值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10426444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
enum set to string and get sting value when need
提问by Darshana
I don't know how to do this
I want code like following
我不知道该怎么做
我想要如下代码
enum myenum
{
name1 = "abc",
name2 = "xyz"
}
and check it
并检查它
if (myenum.name1 == variable)
how can I do those things?
我该怎么做?
thanx.
谢谢。
采纳答案by Jeff Mercado
Unfortunately that's not possible. Enums can only have a basic underlying type (int, uint, short, etc.). If you want to associate the enum values with additional data, apply attributes to the values (such as the DescriptionAttribute).
不幸的是,这是不可能的。枚举只能有一个基本的基本类型(int,uint,short等等)。如果要将枚举值与其他数据相关联,请将属性应用于值(例如DescriptionAttribute)。
public static class EnumExtensions
{
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
public static String GetDescription(this Enum value)
{
var description = GetAttribute<DescriptionAttribute>(value);
return description != null ? description.Description : null;
}
}
enum MyEnum
{
[Description("abc")] Name1,
[Description("xyz")] Name2,
}
var value = MyEnum.Name1;
if (value.GetDescription() == "abc")
{
// do stuff...
}
回答by Matten
This was discuessed here before, but can't find it. Short version: you can't give enum members string values. You can use the member names as the values, but often this isn't what you want. Please follow this linkfor a guide how to use attributes to annotate string values to enum members.
以前在这里讨论过,但找不到。简短版本:您不能为枚举成员提供字符串值。您可以使用成员名称作为值,但这通常不是您想要的。请按照此链接获取如何使用属性将字符串值注释为枚举成员的指南。
回答by npinti
According to herewhat you are doing is not possible. What you could do maybe would be to have a static class full of constants, maybe something like so:
根据这里你正在做的事情是不可能的。你可以做的也许是拥有一个充满常量的静态类,也许是这样的:
class Constants
{
public static string name1 = "abc";
public static string name2 = "xyz";
}
...
if (Constants.name1 == "abc")...
回答by alexg
Depending on what you want to do, maybe you could achieve the same effect by using a Dictionaryinstead of enums.
根据您想要做什么,也许您可以通过使用Dictionary而不是enums来达到相同的效果。
回答by David
Nice answers here. Elaborating onto the suggested answer is if you would like to rather get the enum value given the enum description. I havent tested this but this might work :
不错的答案在这里。详细说明建议的答案是,如果您想获得给定 enum description 的 enum 值。我还没有测试过,但这可能有效:
Enum :
枚举:
public enum e_BootloadSource : byte
{
[EnumMember]
[Display(Name = "UART")]
[Description("UART_BL_RDY4RESET")]
UART = 1,
[EnumMember]
[Display(Name = "SD")]
[Description("SD_BL_RDY4RESET")]
SD = 2,
[EnumMember]
[Display(Name = "USB")]
[Description("USB_BL_RDY4RESET")]
USB = 3,
[EnumMember]
[Display(Name = "Fall Through Mode")]
[Description("FALL_THRGH_BL_RDY4RESET")]
FALL_THROUGH_MODE = 4,
[EnumMember]
[Display(Name = "Cancel Bootload")]
[Description("BL_CANCELED")]
CANCEL_BOOTLOAD = 5,
}
Use as follows :
使用如下:
foreach(e_BootloadSource BLSource in Enum.GetValues(typeof(e_BootloadSource)))
{
if (BLSource.GetDescription() == inputPieces[(int)SetBLFlagIndex.BLSource])
{
newMetadata.BootloadSource = BLSource;
}
}
Note inputpieces is purely a string array and newMetadata.BootloadSource is e_BootloadSource.
注意 inputpieces 纯粹是一个字符串数组,而 newMetadata.BootloadSource 是 e_BootloadSource。

