[英]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.