[英]convert string property of list to enum
如何將列表字母字符串屬性Size正確轉換為枚舉?
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}
};
為什么不使用標准的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);
轉換為字符串,而不是枚舉:
Customer.Letter[i].Size = BusinessObjects.CONSTANTS.dict[Customer.Letter[i].Size].ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.