简体   繁体   English

将列表的字符串属性转换为枚举

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

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

将枚举转换为列表<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> - Convert an enum to List<string> 转换列表 <Enum> 列表 <string> - Convert List<Enum> to List<string> 通用将字符串列表转换为枚举列表 - Genieric convert List of string to list of enum 如何将CSV字符串转换为List <Enum> - How to convert CSV string to 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> - Convert an Enum to a List<object> 将枚举转换为字符串数字 - Convert enum to string digit 无法将字符串转换为枚举 - Cannot convert string to enum 在 function 中将字符串转换为枚举 - Convert string to enum in a function 将整数枚举转换为字符串 - Convert integer enum to string 如何在(字符串,标志列表,带标志的枚举)之间转换枚举? - How do I convert an enum between (string, list of flags, enum with flags)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM