如何将字符串值分配给枚举并在开关中使用该值
基本上,一系列标题将被传递到 switch 语句中,我需要将它们与枚举的字符串值进行比较。但我几乎不知道如何正确地做到这一点。
另外,我不知道这是否是最好的方法,所以是否有人有任何想法?
例如:
enum
{
doctor = "doctor",
mr = "mr",
mrs = "mrs"
}
然后切换我分配给它们的字符串值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我发现执行此操作的最佳方法是在枚举值上使用 System.ComponentModel.DescriptionAttribute 属性。
下面是一个示例:
然后,要使用它,请在静态类上创建一个扩展方法,如下所示:
编辑:我重写了该方法以包含 Laurie Dickinson 的一个很好的建议,以便该方法返回枚举值的名称当没有描述属性时。我还重构了该方法以尝试改进功能。现在,它适用于所有枚举,而无需使用
IConvertible
。因此,要获取与上面的枚举关联的字符串,我们可以使用以下代码:
这是另一个示例枚举:
这是代码查看描述:
结果将是:
我想继续发布通用方法,因为它更有用。它使您不必为所有枚举编写自定义扩展。
I found that the best way for me to do this is by using the
System.ComponentModel.DescriptionAttribute
attribute on the enum values.Here is an example:
Then, to use it, create an extension method on a static class like so:
Edit: I rewrote the method to include a great suggestion from Laurie Dickinson so that the method returns the name of the enum value when there is no Description attribute. I also refactored the method to try to improve functionality. It now works for all Enums without using
IConvertible
.So, to get the string associated with our enum above, we could use the following code:
Here is another sample Enum:
Here is the code to see the description:
Results will be:
I wanted to go ahead and post the generic method as it is much more useful. It prevents you from having to write a custom extension for all of your enums.
您不能使用
enum
string
的基础类型。 基础类型可以是除char
之外的任何整数类型。如果您想将
字符串
转换为枚举
,那么您可能需要使用解析
或TryParse
方法。You can't have an
enum
with an underlying type ofstring
. The underlying type can be any integral type exceptchar
.If you want to translate a
string
to yourenum
then you'll probably need to use theParse
orTryParse
methods.Enum 只能具有整型基础类型(char 除外)。因此你不能做你想做的事,至少不能直接做。
但是,您可以将字符串转换为枚举类型:
Enum can only have integral underlying types (except char). Therefore you cannot do what you want, at least directly.
However you can translate the string you have to the enum type:
我相信执行此操作的标准方法是使用具有只读字符串属性的静态类,该属性返回您想要的值。
I believe the standard way to do this is to use a static class with readonly string properties that return the value you want.
我想为使用 C# 6 或更高版本的任何人添加另一个答案。
如果您只想获取 Enum 值的名称,可以使用 C# 6 中引入的新 nameof() 方法。
虽然乍一看这似乎有些过分(为什么不直接将字符串的值设置为“EnumVal1”) " 首先?),它会给你编译时检查以确保该值有效。因此,如果您更改了枚举值的名称,并且忘记告诉您的 IDE 查找并替换所有引用,则在您修复它们之前它不会编译。
I wanted to add another answer for anyone using C# 6 or greater.
If you are only wanting to get the name of the Enum value, you could use the new nameof() method introduced in C# 6.
While this may seem like overkill at first glance (why not just set the value of the string to "EnumVal1" to start with?), it will give you compile-time checking to make sure the value is valid. So, if you ever change the name of the enum value and forget to tell your IDE to find and replace all references, it will not compile until you fix them.
这不应该是硬编码的事情。它应该是数据驱动的,可能从外部文件或数据库读取。您可以将它们存储在
字典
中,并使用按键来驱动您的逻辑。This is not the kind of thing that should be hard-coded. It should be data-driven, possibly read from an external file or database. You could store them in a
Dictionary
and use the keys to drive your logic.枚举不能是字符串类型。
批准的枚举类型为 byte、sbyte、short、ushort、int、uint、long 或 ulong。
http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
Enumerations cannot be of string type.
The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
为什么不只使用纯枚举和开关?
然后你可以使用就像
Why not to use just pure enum and switches?
Then you can use is like
只是分享我的解决方案。通过下载 nuget 包 Extension.MV,可以使用一种方法从枚举描述中获取字符串
Just sharing my solution. By downloading the nuget package Extension.MV, a method is available for getting the string from a Enum Description
如果你真的是 c# 8 的硬核......
我意识到这不会接受你程序的所有枚举,但这是我使用的并且我喜欢它。
If you're really hardcore and on c# 8...
I realize this won't accept all of your program's enums but it's what I use and I like it.
阅读本教程来了解枚举的工作原理。还有
switch
语句的示例。Have a read of this tutorial to understand how enums work. There are examples of
switch
statements too.