簡體   English   中英

將列表的字符串屬性轉換為枚舉

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

  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();

將枚舉轉換為列表<object><div id="text_translate"><p>首先,為了避免下意識的<em>“這是重復的”</em>陳述:關於序列化枚舉有很多關於 SO 的問題。 但沒有一個(我發現)處理具有描述的枚舉,或者嘗試將枚舉轉換為特定的 object。 (如果你能找到我錯過的一個,那么我會接受這是重復的。)</p><p> 給定一個看起來像這樣的枚舉:</p><pre> public enum OptionType { [Description("Option A")] OptionA = 1, [Description("Option B")] OptionB = 2, Description("Option C")] OptionC= 3, }</pre><p> 我想將其轉換為 JSON 字符串,如下所示:</p><pre> [ { "Id": "1", "Description": "Option A"}, { "Id": "2", "Description": "Option B"}, { "Id": "3", "Description": "Option C"} ]</pre><p> 我有這兩個類:</p><pre> public class EnumList { public List&lt;EnumNameValue&gt; EnumNamesValues { get; set; } } public class EnumNameValue { public int Id { get; set; } public string Description { get; set; } }</pre><p> 我有一個看起來像這樣的調用方法:</p><pre> EnumList enumList = EnumSerializer.EnumToJson&lt;OptionType&gt;();</pre><p> 最后(這是我需要幫助的地方):</p><pre> public static class EnumSerializer { public static EnumList EnumToJson&lt;T&gt;() where T: Enum { Type enumType = typeof(T); // &lt;--- WORKS.; Enum thisEnum1 = (T)Activator.CreateInstance(enumType); // &lt;--- DOES NOT WORK Enum thisEnum2 = (Enum)Activator,CreateInstance(enumType); // &lt;--- DOES NOT WORK // If I can get this far, I *THINK* I can figure it out from here } }</pre><p> 我已經有一個獲取描述的 Enum 擴展,所以我不需要幫助。 我遇到的問題是thisEnum1和thisEnum2最終的值為0</p><p> <a href="https://i.stack.imgur.com/zRatA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zRatA.png" alt="在此處輸入圖像描述"></a></p><p> ( DistrictType是 Enum 的真實名稱。)</p><p> 如何獲得可以循環的枚舉的實際實例?</p></div></object>

[英]Convert an Enum to a List<object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 將枚舉轉換為列表<string> 轉換列表 <Enum> 列表 <string> 通用將字符串列表轉換為枚舉列表 如何將CSV字符串轉換為List <Enum> 將枚舉轉換為列表<object><div id="text_translate"><p>首先,為了避免下意識的<em>“這是重復的”</em>陳述:關於序列化枚舉有很多關於 SO 的問題。 但沒有一個(我發現)處理具有描述的枚舉,或者嘗試將枚舉轉換為特定的 object。 (如果你能找到我錯過的一個,那么我會接受這是重復的。)</p><p> 給定一個看起來像這樣的枚舉:</p><pre> public enum OptionType { [Description("Option A")] OptionA = 1, [Description("Option B")] OptionB = 2, Description("Option C")] OptionC= 3, }</pre><p> 我想將其轉換為 JSON 字符串,如下所示:</p><pre> [ { "Id": "1", "Description": "Option A"}, { "Id": "2", "Description": "Option B"}, { "Id": "3", "Description": "Option C"} ]</pre><p> 我有這兩個類:</p><pre> public class EnumList { public List&lt;EnumNameValue&gt; EnumNamesValues { get; set; } } public class EnumNameValue { public int Id { get; set; } public string Description { get; set; } }</pre><p> 我有一個看起來像這樣的調用方法:</p><pre> EnumList enumList = EnumSerializer.EnumToJson&lt;OptionType&gt;();</pre><p> 最后(這是我需要幫助的地方):</p><pre> public static class EnumSerializer { public static EnumList EnumToJson&lt;T&gt;() where T: Enum { Type enumType = typeof(T); // &lt;--- WORKS.; Enum thisEnum1 = (T)Activator.CreateInstance(enumType); // &lt;--- DOES NOT WORK Enum thisEnum2 = (Enum)Activator,CreateInstance(enumType); // &lt;--- DOES NOT WORK // If I can get this far, I *THINK* I can figure it out from here } }</pre><p> 我已經有一個獲取描述的 Enum 擴展,所以我不需要幫助。 我遇到的問題是thisEnum1和thisEnum2最終的值為0</p><p> <a href="https://i.stack.imgur.com/zRatA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zRatA.png" alt="在此處輸入圖像描述"></a></p><p> ( DistrictType是 Enum 的真實名稱。)</p><p> 如何獲得可以循環的枚舉的實際實例?</p></div></object> 將枚舉轉換為字符串數字 無法將字符串轉換為枚舉 在 function 中將字符串轉換為枚舉 將整數枚舉轉換為字符串 如何在(字符串,標志列表,帶標志的枚舉)之間轉換枚舉?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM