简体   繁体   English

将枚举转换为另一个枚举

[英]Convert Enum into another Enum

I have two enums:我有两个枚举:

public enum Fruits
{
    Apple = 1,
    Banana = 2,
    Strawberry = 3,
    Blueberry = 4
}

public enum FruitsCombined
{
    Apple = 1,
    Banana = 2,
    Berries = 0  //Strawberry and Blueberry combined
}

I have a Combobox, which is bound to the FruitsCombined enum, but in the background, when I select Berries , I want to say it's 3 and 4 from the Fruits enum.我有一个 Combobox,它绑定到FruitsCombined枚举,但在后台,当我 select Berries时,我想说它是Fruits枚举中的 3 和 4。

How can I do this?我怎样才能做到这一点?

For example, I want to replace this for a better way, with the two enums:例如,我想用两个枚举替换它以获得更好的方法:

if ((int)cboFilterFruitType.SelectedValue == 0)
{
    order = order.FindAll(o => o.FruitID == 3 || o.FruitID == 4);
}
else
{
    order = order.FindAll(o => o.FruitID == (int) cboFilterFruitType.SelectedValue);
}

You can use flags enums:您可以使用flags枚举:

[Flags]
public enum Fruits
{
    Apple = 1,
    Banana = 2,
    Strawberry = 4,
    Blueberry = 8
}

public enum FruitsCombined
{
    Apple = 1,
    Banana = 2,
    Berries = Fruits.Strawberry | Fruits.Blueberry //Strawberry and Blueberry combined
}

Now you can cast one to the other:现在您可以将一个转换为另一个:

FruitsCombined fruitCombined = FruitsCombined.Berries;
Fruits fruit = (Fruits)fruitCombined; // Fruits.Strawberry | Fruits.Blueberry

If you want to check if it's a berry you can use Enum.HasFlag :如果你想检查它是否是浆果,你可以使用Enum.HasFlag

bool isBerry = fruit.HasFlag(Fruits.Strawberry | Fruits.Blueberry);

If you have control over both enums you could combine the enums together using flags like suggested in the comments.如果您可以控制这两个枚举,则可以使用评论中建议的标志将这些枚举组合在一起。

[Flags]
public enum Fruits
{
    None = 0,
    Apple = 1,
    Banana = 2,
    Strawberry = 4,
    Blueberry = 8,
    Berries = Strawberry | Blueberry
}

Then if you had code like this那么如果你有这样的代码

var fruit = Fruits.Berries;
Console.WriteLine(fruit.HasFlag(Strawberry));
Console.WriteLine(fruit.HasFlag(Blueberry));

Both write lines would print true.两条写入行都将打印为真。

as stuartd link says do something more like正如 stuartd 链接所说,做一些更像

change to powers of two变为二的幂

public enum Fruits
{
    Apple = 1,
    Banana = 2,
    Strawberry = 4,
    Blueberry = 8
}

Then you can use bitwise to represent any combo然后你可以使用 bitwise 来表示任何组合

var appBann = Fruits.Apple | var appBann = Fruits.Apple | Suits.Banana; Suits.Banana;

or Berries = Fruits.Strawberry |或浆果 = Fruits.Strawberry | Suits.Blueberry西装.蓝莓

you can even have it as a enum你甚至可以把它作为一个枚举

  public enum Fruits
    {
        Apple = 1,
        Banana = 2,
        Strawberry = 4,
        Blueberry = 8,
        Berries = Strawberry | Blueberry
    }
 order = order.FindAll(o => o.FruitID == (int)Fruits.Berries);

将枚举转换为列表<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.

相关问题 将枚举转换为另一种类型的枚举 - convert an enum to another type of enum 将标记的枚举转换为另一个枚举 - Convert a flagged enum to another one 如何将作为通用参数的Enum转换为另一个Enum? - How to convert an Enum which is a generic parameter to another Enum? 在C#中将标志枚举转换为另一个标志枚举 - Convert a Flag Enum to another Flag Enum in C# 有没有一种方法可以在一个自定义枚举集合与另一个自定义枚举集合之间进行转换? - Is there a way to convert between a collection of custom enum to a collection of another custom enum? 是否可以获取一个枚举列表并将其转换为另一个? - Is It possible to take one enum list and convert it to another? 无法将UserQuery枚举转换为枚举 - Cannot Convert UserQuery enum to enum 枚举中的“&”? - “&” in an 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 integer enum to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM