C#获取枚举值 [英] C# Getting Enum values
问题描述
我有一个包含枚举以下(例如):
I have a enum containing the following (for example):
- UnitedKingdom的买家,
- UnitedStates的,
- 在法国,
- 葡萄牙
在我的code我使用 Country.UnitedKingdom ,但我想有值英国如果我给它一个字符串例如。
In my code I use Country.UnitedKingdom but I want to have the value be UK if I assign it to a string for example.
这可能吗?
推荐答案
您不能枚举值分配给一个字符串的开始。你得叫的ToString()
,这将转换成 Country.UnitedKingdom
来来自国家UnitedStates。
You can't assign an enum value to a string to start with. You'd have to call ToString()
, which would convert Country.UnitedKingdom
to "UnitedKingdom".
有两种选择自己的建议:
Two options suggest themselves:
- 创建一个
词典<国家,串>
- 在switch语句
- 在每一个装饰用的价值属性,并加载与反思
有关他们每个人的意见......
Comments about each of them...
样品$ C $下词典<国家,串>
Sample code for Dictionary<Country,string>
using System;
using System.Collections.Generic;
enum Country
{
UnitedKingdom,
UnitedStates,
France,
Portugal
}
class Test
{
static readonly Dictionary<Country, string> CountryNames =
new Dictionary<Country, string>
{
{ Country.UnitedKingdom, "UK" },
{ Country.UnitedStates, "US" },
};
static string ConvertCountry(Country country)
{
string name;
return (CountryNames.TryGetValue(country, out name))
? name : country.ToString();
}
static void Main()
{
Console.WriteLine(ConvertCountry(Country.UnitedKingdom));
Console.WriteLine(ConvertCountry(Country.UnitedStates));
Console.WriteLine(ConvertCountry(Country.France));
}
}
您可能希望把 ConvertCountry
逻辑到一个扩展方法。例如:
You might want to put the logic of ConvertCountry
into an extension method. For example:
// Put this in a non-nested static class
public static string ToBriefName(this Country country)
{
string name;
return (CountryNames.TryGetValue(country, out name))
? name : country.ToString();
}
然后,你可以写:
Then you could write:
string x = Country.UnitedKingdom.ToBriefName();
正如在评论中,默认的字典比较器将包括拳击,这是不理想的。对于一次性的,我会住在一起,直到我发现这是一个瓶颈。如果我要多枚举这样做,我会写一个可重用的类。
As mentioned in the comments, the default dictionary comparer will involve boxing, which is non-ideal. For a one-off, I'd live with that until I found it was a bottleneck. If I were doing this for multiple enums, I'd write a reusable class.
switch语句
我同意 使用开关暗示yshuditelu的答案
语句比较少的情况。然而,由于每个案件都将是一个单独的语句,我个人改变我的编码风格针对这种情况,保持code紧凑,但可读的:
I agree with yshuditelu's answer suggesting using a switch
statement for relatively few cases. However, as each case is going to be a single statement, I'd personally change my coding style for this situation, to keep the code compact but readable:
public static string ToBriefName(this Country country)
{
switch (country)
{
case Country.UnitedKingdom: return "UK";
case Country.UnitedStates: return "US";
default: return country.ToString();
}
}
您可以添加更多的情况下,这个没有它变得过于庞大,而且很容易从对面枚举值蒙上你的眼睛的返回值。
You can add more cases to this without it getting too huge, and it's easy to cast your eyes across from enum value to the return value.
DescriptionAttribute
DescriptionAttribute
href="http://stackoverflow.com/questions/1008090/c-enum-values/1008139#1008139">瑞士雷达表作出有关$ C $下<$ C $点 DescriptionAttribute 是可重复使用的是一个很好的,但在这种情况下,我建议你不要使用反射每次你需要得到一个值的时间。我可能会写一个通用的静态类保持一个查找表(可能是一个词典
,可能与中提到的注释的自定义比较)。扩展方法不能在泛型类中定义,所以你可能最终的东西,如:
The point Rado made about the code for DescriptionAttribute
being reusable is a good one, but in that case I'd recommend against using reflection every time you need to get a value. I'd probably write a generic static class to hold a lookup table (probably a Dictionary
, possibly with a custom comparer as mentioned in the comments). Extension methods can't be defined in generic classes, so you'd probably end up with something like:
public static class EnumExtensions
{
public static string ToDescription<T>(this T value) where T : struct
{
return DescriptionLookup<T>.GetDescription(value);
}
private static class DescriptionLookup<T> where T : struct
{
static readonly Dictionary<T, string> Descriptions;
static DescriptionLookup()
{
// Initialize Descriptions here, and probably check
// that T is an enum
}
internal static string GetDescription(T value)
{
string description;
return Descriptions.TryGetValue(value, out description)
? description : value.ToString();
}
}
}
这篇关于C#获取枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!