[英]Dictionary of Enum Values as strings
我正在尝试创建一个API,该API中的一个函数将Enum作为参数,然后对应于使用的字符串。
public enum PackageUnitOfMeasurement
{
LBS,
KGS,
};
对此进行编码的简单方法必须列出代码中的每个案例。 但是因为它们是30个案例,所以我试图避免这种情况并使用Dictionary Data Structure
,但我似乎无法连接点如何将值与枚举相关联。
if(unit == PackageUnitOfMeasurement.LBS)
uom.Code = "02"; //Please note this value has to be string
else if (unit == PackageUnitOfMeasurement.KGS)
uom.Code = "03";
以下是将映射存储在字典中并稍后检索值的一种方法:
var myDict = new Dictionary<PackageUnitOfMeasurement,string>();
myDict.Add(PackageUnitOfMeasurement.LBS, "02");
...
string code = myDict[PackageUnitOfMeasurement.LBS];
另一种选择是使用类似DecriptionAttribute
东西来装饰每个枚举项,并使用反射来读出这些,如获取Enum值的属性中所述:
public enum PackageUnitOfMeasurement
{
[Description("02")]
LBS,
[Description("03")]
KGS,
};
var type = typeof(PackageUnitOfMeasurement);
var memInfo = type.GetMember(PackageUnitOfMeasurement.LBS.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),
false);
var description = ((DescriptionAttribute)attributes[0]).Description;
第二种方法的好处是您可以使映射保持接近枚举,如果有任何更改,则无需寻找需要更新的任何其他位置。
您可以指定枚举元素的数字值
public enum PackageUnitOfMeasurement {
None = 0,
LBS = 2,
KGS = 3,
TONS = 0x2a
};
然后你可以简单地转换单位
uom.Code = ((int)unit).ToString("X2");
注意:
根本问题是,对单元进行硬编码是否是一个好主意。 通常,这类内容应放入数据库中的查找表中,这样可以随时轻松添加新单元,而无需更改程序代码。
更新:
我添加了一个包含HEX代码的示例。 格式“X2”产生两位十六进制值。 以十六进制表示法输入大于9的所有数字,如0xA == 10
0x10 == 16
in c#。
我会使用附加到枚举的属性。 像这样:
var type = typeof(PackageUnitOfMeasurement);
var memInfo = type.GetMember(PackageUnitOfMeasurement.LBS.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),
false);
var description = ((DescriptionAttribute)attributes[0]).Description;
这取自这个问题, 获得Enum值的属性 。 其中特别要求返回枚举属性。
Oded的解决方案很好,但我过去使用的替代方法是使用附加到包含相应字符串的枚举值的属性。
这就是我做的。
public class DisplayStringAttribute : Attribute
{
private readonly string value;
public string Value
{
get { return value; }
}
public DisplayStringAttribute(string val)
{
value = val;
}
}
然后我可以像这样定义我的枚举:
public enum MyState
{
[DisplayString("Ready")]
Ready,
[DisplayString("Not Ready")]
NotReady,
[DisplayString("Error")]
Error
};
而且,为了我的目的,我创建了一个转换器,所以我可以绑定到枚举:
public class EnumDisplayNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Type t = value.GetType();
if (t.IsEnum)
{
FieldInfo fi = t.GetField(value.ToString());
DisplayStringAttribute[] attrbs = (DisplayStringAttribute[])fi.GetCustomAttributes(typeof(DisplayStringAttribute),
false);
return ((attrbs.Length > 0) && (!String.IsNullOrEmpty(attrbs[0].Value))) ? attrbs[0].Value : value.ToString();
}
else
{
throw new NotImplementedException("Converter is for enum types only");
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Oded的解决方案可能表现得更快,但该解决方案具有很大的灵活性。 如果你真的想要,你可以添加一大堆属性。 但是,如果你这样做,你可能最好不要创建一个类而不是使用枚举!
PackageUnitOfMeasurement.LBS
的默认值为0,同样PackageUnitOfMeasurement.KBS
为1 。
所以PackageUnitOfMeasurement
的集合PackageUnitOfMeasurement
是一个包含整数值(即int
)的集合。
你的问题并不完全清楚......
Dictionary集合有一个Key
和一个Value
它听起来像你想要使用PackageUnitOfMeasurement有一个Key
,这个值很简单,就像将值转换为整数一样。
PackageUnitOfMeasurement example = PackageUnitOfMeasurement.LBS
int result = (int)example;
var myDict = new Dictionary<PackageUnitOfMeasurement,string>();
myDict.Add(result,"some value here"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.