我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
当前回答
为什么不使用相同的枚举,而只是调用.ToString()?
using System;
public class EnumSample
{
enum Holidays
{
Christmas = 1,
Easter = 2
};
public static void Main()
{
Enum myHolidays = Holidays.Christmas;
Console.WriteLine("The value of this instance is '{0}'", myHolidays.ToString());
}
}
其他回答
为你的DB创建第二个枚举,包含以下内容:
enum DBGroupTypes
{
OEM = 0,
CMB = 1
}
现在,您可以使用Enum。解析以从字符串“OEM”和“CMB”中检索正确的DBGroupTypes值。然后,您可以将这些值转换为int,并从您希望在模型中进一步使用的正确枚举中检索正确的值。
添加这个类
public class DatabasePreference {
public DatabasePreference([CallerMemberName] string preferenceName = "") {
PreferenceName = preferenceName;
}
public string PreferenceName;
}
这项工作使用CallerMemberName来最小化编码
使用:
//Declare names
public static DatabasePreference ScannerDefaultFlashLight = new DatabasePreference();
public static DatabasePreference ScannerQrCodes = new DatabasePreference();
public static DatabasePreference Scanner1dCodes = new DatabasePreference();
测试:
Console.WriteLine(ScannerDefaultFlashLight.PreferenceName);
Console.WriteLine(ScannerDefaultFlashLight.Scanner1dCodes);
输出:
ScannerDefaultFlashLight
Scanner1dCodes
Glennular Extension方法的一个小调整,所以你可以在其他东西上使用扩展,而不仅仅是ENUM的;
using System;
using System.ComponentModel;
namespace Extensions {
public static class T_Extensions {
/// <summary>
/// Gets the Description Attribute Value
/// </summary>
/// <typeparam name="T">Entity Type</typeparam>
/// <param name="val">Variable</param>
/// <returns>The value of the Description Attribute or an Empty String</returns>
public static string Description<T>(this T t) {
DescriptionAttribute[] attributes = (DescriptionAttribute[])t.GetType().GetField(t.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
}
}
或者使用Linq
using System;
using System.ComponentModel;
using System.Linq;
namespace Extensions {
public static class T_Extensions {
public static string Description<T>(this T t) =>
((DescriptionAttribute[])t
?.GetType()
?.GetField(t?.ToString())
?.GetCustomAttributes(typeof(DescriptionAttribute), false))
?.Select(a => a?.Description)
?.FirstOrDefault()
?? string.Empty;
}
}
如果你喜欢自己编写扩展,上面有几个很好的答案
我使用CodeHelper.Core.Extensions
enum GroupTypes
{
[StringValue("OEM")] TheGroup,
[StringValue("CMB")] TheOtherGroup = "CMB"
}
GroupTypes.TheOtherGroup.ToStringValue()
如果你没有将StringValue属性添加到值中,扩展将返回正常名称(.ToTring()) 并且超级容易添加空格和其他通常不允许的字符,如空格或以数字开头
我要分享一个不同的答案。我想发送一个字符串来返回一个字符串。例如: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”