我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
当前回答
我要分享一个不同的答案。我想发送一个字符串来返回一个字符串。例如:Airport = A, Railway = R。
public enum LocationType
{
AIRPORT = 1,
RAILWAY = 2,
DOWNTOWN = 3
}
public enum ShortLocationType
{
A = 1,
R = 2,
D = 3
}
((ShortLocationType)(int)Enum.Parse(typeof(LocationType), "AIRPORT")).ToString();
输出“”
((ShortLocationType)(int)Enum.Parse(typeof(LocationType), "DOWNTOWN")).ToString();
输出“D”
其他回答
你可以很容易地做到。使用下面的代码。
enum GroupTypes
{
OEM,
CMB
};
然后,当您想获取每个枚举元素的字符串值时,只需使用下面的代码行。
String oemString = Enum.GetName(typeof(GroupTypes), GroupTypes.OEM);
我曾经成功地使用过这个方法,我也使用过一个常量类来保存字符串常量,两者都很好,但我倾向于这样做。
这里是扩展方法,我用来获得枚举值作为字符串。首先是枚举。
public enum DatabaseEnvironment
{
[Description("AzamSharpBlogDevDatabase")]
Development = 1,
[Description("AzamSharpBlogQADatabase")]
QualityAssurance = 2,
[Description("AzamSharpBlogTestDatabase")]
Test = 3
}
Description属性来自System.ComponentModel。
这是我的扩展方法:
public static string GetValueAsString(this DatabaseEnvironment environment)
{
// get the field
var field = environment.GetType().GetField(environment.ToString());
var customAttributes = field.GetCustomAttributes(typeof (DescriptionAttribute), false);
if(customAttributes.Length > 0)
{
return (customAttributes[0] as DescriptionAttribute).Description;
}
else
{
return environment.ToString();
}
}
现在,你可以使用下面的代码访问枚举字符串值:
[TestFixture]
public class when_getting_value_of_enum
{
[Test]
public void should_get_the_value_as_string()
{
Assert.AreEqual("AzamSharpBlogTestDatabase",DatabaseEnvironment.Test.GetValueAsString());
}
}
我不需要任何健壮的东西,比如将字符串存储在属性中。我只需要转动MyEnum之类的东西。BillEveryWeek变成“每周账单”或MyEnum。将elegacysystem转换为“使用遗留系统”——基本上通过驼峰式大小写将枚举拆分为单个小写单词。
public static string UnCamelCase(this Enum input, string delimiter = " ", bool preserveCasing = false)
{
var characters = input.ToString().Select((x, i) =>
{
if (i > 0 && char.IsUpper(x))
{
return delimiter + x.ToString(CultureInfo.InvariantCulture);
}
return x.ToString(CultureInfo.InvariantCulture);
});
var result = preserveCasing
? string.Concat(characters)
: string.Concat(characters).ToLower();
var lastComma = result.LastIndexOf(", ", StringComparison.Ordinal);
if (lastComma > -1)
{
result = result.Remove(lastComma, 2).Insert(lastComma, " and ");
}
return result;
}
myenume . uselegacysystem . uncamelcase()输出"使用遗留系统"
如果设置了多个标志,它会将其转换为普通英语(逗号分隔,除了最后一个逗号中的“and”)。
var myCustomerBehaviour = MyEnum.BillEveryWeek | MyEnum.UseLegacySystem | MyEnum.ChargeTaxes;
Console.WriteLine(myCustomerBehaviour.UnCamelCase());
//outputs "bill every week, use legacy system and charge taxes"
我只需创建一个字典,并使用代码作为键。
编辑:为了解决关于执行反向查找(查找键)的注释,这将不是非常有效。如果有必要,我将编写一个新类来处理它。
我要分享一个不同的答案。我想发送一个字符串来返回一个字符串。例如:Airport = A, Railway = R。
public enum LocationType
{
AIRPORT = 1,
RAILWAY = 2,
DOWNTOWN = 3
}
public enum ShortLocationType
{
A = 1,
R = 2,
D = 3
}
((ShortLocationType)(int)Enum.Parse(typeof(LocationType), "AIRPORT")).ToString();
输出“”
((ShortLocationType)(int)Enum.Parse(typeof(LocationType), "DOWNTOWN")).ToString();
输出“D”