C# 将多个枚举合并到主枚举列表中

C# 将多个枚举合并到主枚举列表中,c#,enums,combinations,C#,Enums,Combinations,是否可以将多个枚举组合在一起?下面是我希望看到的代码示例: enum PrimaryColors { Red, Yellow, Blue } enum SecondaryColors { Orange, Green, Purple } //Combine them into a new enum somehow to result in: enum AllColors { Red, Orange, Yellow, Green,

是否可以将多个枚举组合在一起?下面是我希望看到的代码示例:

enum PrimaryColors
{
   Red,
   Yellow,
   Blue
}

enum SecondaryColors
{
   Orange,
   Green,
   Purple
}

//Combine them into a new enum somehow to result in:
enum AllColors
{
   Red,
   Orange,
   Yellow,
   Green,
   Blue,
   Purple
}
无论它们的顺序是什么,或者它们的支持号码是什么,我只希望能够将它们组合起来

对于上下文,这是为了使我正在处理的程序的多个类都有与它们的工作相关联的枚举。我的主程序将从每个支持类中读取所有可用的枚举,并生成可用命令的可用枚举的主列表(枚举用于)

编辑: 产生这些枚举的原因是因为我的主程序正在读取在特定时间执行的命令列表,因此我想读取文件,查看其中的命令是否与我的枚举之一关联,如果关联,则将其放入要执行的命令列表中

产生这些枚举的原因是因为我的主程序正在读取在特定时间执行的命令列表,因此我想读取文件,查看其中的命令是否与我的枚举之一关联,如果关联,则将其放入要执行的命令列表中

这似乎不需要三种不同的
enum
类型,只需要一种类型(您称之为“master
enum
”)外加一些方法来确定某个值属于哪个子枚举。要做到这一点,您可以使用主枚举中的一组值,或者使用
开关

例如:

enum Color
{
   Red,
   Orange,
   Yellow,
   Green,
   Blue,
   Purple
}

bool IsPrimaryColor(Color color)
{
    switch (color)
    {
    case Color.Red:
    case Color.Yellow:
    case Color.Blue:
        return true;
    default:
        return false;
    }
}

另外,(除非它是一个标志
enum
)。

我不确定我是否准确理解。但您可以将所有值列成一个
列表,如下所示:

var allColors = new List<Enum>();

allColors.AddRange(Enum.GetValues(typeof(PrimaryColors)).Cast<Enum>());
allColors.AddRange(Enum.GetValues(typeof(SecondaryColors)).Cast<Enum>());
var allColors=new List();
allColors.AddRange(Enum.GetValues(typeof(PrimaryColors)).Cast());
allColors.AddRange(Enum.GetValues(typeof(secondaryColor)).Cast());

您也可以使用
HashSet
,而不是
List
。在任何情况下,由于您将
PrimaryColor
SecondaryColor
分配给类类型(即
System.Enum
),您得到了,但这只是一个技术细节(可能)。

保持简单,只需使用隐式
int
转换,或使用
System.Enum.Parse()
函数:

enum PrimaryColors
{        
    Red = 0,
    Yellow = 2,
    Blue = 4
}

enum SecondaryColors
{
    Orange = 1,
    Green = 3,
    Purple = 5
}

//Combine them into a new enum somehow to result in:
enum AllColors
{
    Red = 0,
    Orange,
    Yellow,
    Green,
    Blue,
    Purple
}

class Program
{
    static AllColors ParseColor(Enum color)
    {
        return ParseColor(color.ToString());
    }
    static AllColors ParseColor(string color)
    {
        return (AllColors)Enum.Parse(typeof(AllColors), color);
    }
    static void Main(string[] args)
    {
        PrimaryColors color1=PrimaryColors.Red;
        AllColors result=(AllColors)color1;
        // AllColors.Red

        SecondaryColors color2=SecondaryColors.Green;
        AllColors other=(AllColors)color2; 
        // AllColors.Green

        AllColors final=ParseColor(PrimaryColors.Yellow);
        // AllColors.Yellow
    }
}

您可以从完整组派生子组。
例如:

enum AllColors 
{
   Red,
   Orange,
   Yellow,
   Green,
   Blue,
   Purple
}

enum PrimaryColors
{
   Red = AllColors.Red,
   Yellow = AllColors.Yellow,
   Blue = AllColors.Blue
}

enum SecondaryColors
{
   Orange = AllColors.Orange,
   Green = AllColors.Green,
   Purple = AllColors.Purple
}

enum
基本上是一个编译时概念。您是否希望生成一个包含类似于
AllColors
的定义的C#文件,或者您正在尝试执行其他操作?您是否试图以编程方式创建组合类型
AllColors
,我的意思是,在应用程序运行之前不创建它?如果只想将它们添加到列表中,请记住所有枚举都有
System.Object
System.Enum
作为它们的基类。您可以基于其中一个类创建一些列表。当然,您的枚举值会被装箱,但这可能不是问题。为什么要将自己限制在
enum
?@dasblinkenlight我基本上是在制作一个程序,通过支持文件处理不同的事情。因此,如果“legs”枚举的值为“walk”和“dance”,而arms枚举的值为“grass”和“wave”,那么我希望四肢枚举知道可能的动作是“walk”、“dance”、“grass”和“wave”。我试图在这些支持文件中保留与某些支持文件关联的枚举,以减少主程序与其支持文件之间的耦合。为什么不简单地使用字符串列表,其中每个字符串表示一个命令?顺便说一句,
HashSet
作为性能优化,在这里很可能没有意义。但是如果你用它来让意图更清楚的话,它可能是有意义的。当我尝试它时,这段代码似乎不起作用。在基于此的示例程序中,我得到了错误:
System.Collections.Generic.List.AddRange(System.Collections.Generic.IEnumerable)的最佳重载方法匹配'具有一些无效参数
参数1:无法从'System.Collections.Generic.IEnumerable'转换为'System.Collections.Generic.IEnumerable'
@svick Agree。顺便说一下,不同类型的散列码发生冲突的风险很大,例如
红色
橙色
发生冲突,等等。所以这不是为了它的表现。然而,实例方法
Equals(object)
工作正常(它被枚举覆盖,以检查参数的类型,然后检查基础整数值是否一致)。这是可行的,但如果使用相反的方法,即从子组派生组,请小心;除非将非重叠的int值指定给子组项,否则将在完整组中获得重叠项。从技术上讲,这并不违法,但如果你没有预料到的话,这是一个潜在的问题。