[英]convert string property of list to enum
How can I convert my list Letter string property Size to an enum correctly? 如何将列表字母字符串属性Size正确转换为枚举?
Customer.Letter.Size is type string. 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
That is my enum: 那是我的列举:
public enum E_Size {
small = 1,
medium = 2,
big = 3
}
Here my dictionary: 这是我的字典:
public static Dictionary<string, E_Size> dict = new Dictionary<string, E_Size> {
{ "small", E_Size.small},
{ "medium", E_Size.medium},
{ "big", E_Size.big}
};
Why not using standard Enum.Parse
? 为什么不使用标准的Enum.Parse
?
String source = "small";
// E_Size.small
E_Size size = (E_Size) (Enum.Parse(typeof(E_Size), source));
Reverse conversion (from E_Size
to String
) either 反向转换(从E_Size
到String
)
E_Size source = E_Size.small;
String result = source.ToString();
or 要么
E_Size source = E_Size.small;
String result = Enum.GetName(typeof(E_Size), source);
转换为字符串,而不是枚举:
Customer.Letter[i].Size = BusinessObjects.CONSTANTS.dict[Customer.Letter[i].Size].ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.