我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
当前回答
添加这个类
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
其他回答
另一种处理这个问题的方法是,有一个枚举和一个字符串数组,将枚举值映射到字符串列表:
public enum GroupTypes
{
TheGroup = 0,
TheOtherGroup
}
string[] GroupTypesStr = {
"OEM",
"CMB"
};
你可以这样使用它:
Log.Write(GroupTypesStr[(int)GroupTypes.TheOtherGroup]);
它将提示CMB
优点:
简单干净的代码。 高性能(特别是与那些方法相比 使用类)
缺点:
易于混乱的列表时,编辑它,但这将是一个好的 短名单。
我做过类似的事情;
public enum BusinessUnits
{
NEW_EQUIPMENT = 0,
USED_EQUIPMENT = 1,
RENTAL_EQUIPMENT = 2,
PARTS = 3,
SERVICE = 4,
OPERATOR_TRAINING = 5
}
public class BusinessUnitService
{
public static string StringBusinessUnits(BusinessUnits BU)
{
switch (BU)
{
case BusinessUnits.NEW_EQUIPMENT: return "NEW EQUIPMENT";
case BusinessUnits.USED_EQUIPMENT: return "USED EQUIPMENT";
case BusinessUnits.RENTAL_EQUIPMENT: return "RENTAL EQUIPMENT";
case BusinessUnits.PARTS: return "PARTS";
case BusinessUnits.SERVICE: return "SERVICE";
case BusinessUnits.OPERATOR_TRAINING: return "OPERATOR TRAINING";
default: return String.Empty;
}
}
}
用这个称呼它;
BusinessUnitService.StringBusinessUnits(BusinessUnits.PARTS)
这是一种将它用作强类型参数或字符串的方法:
public class ClassLikeEnum
{
public string Value
{
get;
private set;
}
ClassLikeEnum(string value)
{
Value = value;
}
public static implicit operator string(ClassLikeEnum c)
{
return c.Value;
}
public static readonly ClassLikeEnum C1 = new ClassLikeEnum("RandomString1");
public static readonly ClassLikeEnum C2 = new ClassLikeEnum("RandomString2");
}
. net Core 3.0/ c# 8.0(如果你的工作环境允许你升级你的项目)新增了一个简短的switch语句,看起来有点枚举。在一天结束的时候,这和我们多年来一直在使用的无聊的开关语句是一样的。
这里唯一真正的区别是switch语句得到了一个新的suit。
public static RGBColor FromRainbow(Rainbow colorBand) =>
colorBand switch
{
Rainbow.Red => new RGBColor(0xFF, 0x00, 0x00),
Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
Rainbow.Green => new RGBColor(0x00, 0xFF, 0x00),
Rainbow.Blue => new RGBColor(0x00, 0x00, 0xFF),
Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
_ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
};
你会注意到上面我从这里复制的代码,实际上是使用一个枚举作为参数。
这并不是您想要的(相信我,我早就想要类似OP要求的东西了),但我实际上觉得这是MS. JMO抛出的橄榄枝。
希望它能帮助到一些人!
您是否考虑过使用字典查找表?
enum GroupTypes
{
TheGroup,
TheOtherGroup
}
Dictionary<string, GroupTypes> GroupTypeLookup = new Dictionary<string, GroupTypes>();
// initialize lookup table:
GroupTypeLookup.Add("OEM", TheGroup);
GroupTypeLookup.Add("CMB", TheOtherGroup);
然后,您可以使用GroupTypeLookup.TryGetValue()在读取字符串时查找它。