我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
我知道下面是不可能的,因为枚举的类型必须是int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
从我的数据库中,我得到了一个不全面的代码字段(OEM和CMBs)。我想让这个字段成为一个枚举或其他可以理解的东西。因为如果目标是可读性,解决方案就应该简洁。
我还有其他选择吗?
当前回答
public class DataType
{
private readonly string value;
private static readonly Dictionary<string, DataType> predefinedValues;
public static readonly DataType Json = new DataType("json");
public static readonly DataType Xml = new DataType("xml");
public static readonly DataType Text = new DataType("text");
public static readonly DataType Html = new DataType("html");
public static readonly DataType Binary = new DataType("binary");
static DataType()
{
predefinedValues = new Dictionary<string, DataType>();
predefinedValues.Add(Json.Value, Json);
predefinedValues.Add(Xml.Value, Xml);
predefinedValues.Add(Text.Value, Text);
predefinedValues.Add(Html.Value, Html);
predefinedValues.Add(Binary.Value, Binary);
}
private DataType(string value)
{
this.value = value;
}
public static DataType Parse(string value)
{
var exception = new FormatException($"Invalid value for type {nameof(DataType)}");
if (string.IsNullOrEmpty(value))
throw exception;
string key = value.ToLower();
if (!predefinedValues.ContainsKey(key))
throw exception;
return predefinedValues[key];
}
public string Value
{
get { return value; }
}
}
其他回答
摘自@EvenMien,并添加了一些评论。(也适用于我自己的用例)
public struct AgentAction
{
private AgentAction(string value) { Value = value; }
public string Value { get; private set; }
public override string ToString() { return this.Value; }
public static AgentAction Login = new AgentAction("Login");
public static AgentAction Logout = new AgentAction("Logout");
public static implicit operator string(AgentAction action) { return action.ToString(); }
}
另一种处理这个问题的方法是,有一个枚举和一个字符串数组,将枚举值映射到字符串列表:
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)
如果我理解正确,你需要一个从字符串到enum的转换:
enum GroupTypes {
Unknown = 0,
OEM = 1,
CMB = 2
}
static GroupTypes StrToEnum(string str){
GroupTypes g = GroupTypes.Unknown;
try {
object o = Enum.Parse(typeof(GroupTypes), str, true);
g = (GroupTypes)(o ?? 0);
} catch {
}
return g;
}
// then use it like this
GroupTypes g1 = StrToEnum("OEM");
GroupTypes g2 = StrToEnum("bad value");
如果您愿意,可以使用枚举类型的泛型使其更加花哨。
使用带有常量的静态类如何?
static class GroupTypes
{
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
void DoSomething(string groupType)
{
if(groupType == GroupTypes.TheGroup)
{
// Be nice
}
else if (groupType == GroupTypes.TheOtherGroup)
{
// Continue to be nice
}
else
{
// unexpected, throw exception?
}
}