如何将我的列表字母字符串属性大小正确转换为枚举?
Customer.Letter.Size是类型字符串。
Customer.Letter[i].Size = (BusinessObjects.CONSTANTS.E_Letters)BusinessObjects.CONSTANTS.dict[Customer.Letter[i].Size];//i variable declared in an for loop
这是我的枚举:
public enum E_Size {
small = 1,
medium = 2,
big = 3
}
这是我的词典:
public static Dictionary<string, E_Size> dict = new Dictionary<string, E_Size> {
{ "small", E_Size.small},
{ "medium", E_Size.medium},
{ "big", E_Size.big}
};
答案 0 :(得分:2)
为什么不使用标准Enum.Parse
?
String source = "small";
// E_Size.small
E_Size size = (E_Size) (Enum.Parse(typeof(E_Size), source));
反转换(从E_Size
到String
)
E_Size source = E_Size.small;
String result = source.ToString();
或
E_Size source = E_Size.small;
String result = Enum.GetName(typeof(E_Size), source);
答案 1 :(得分:1)
转换为字符串,而不是枚举:
Customer.Letter[i].Size = BusinessObjects.CONSTANTS.dict[Customer.Letter[i].Size].ToString();