我为这个问题的标题道歉,但我想不出更好的方式来描述我想要完成的事情。
我想使用Flags属性创建一个Enum,其中枚举的按位值会创建一些继承模式。
示例:假设我们有一点食物的字段:
[Flags]
enum Foods
{
// Top Level foods
Meat,
Fruit,
Vegetables,
Dairy,
Grain,
BenJerrysOatmealCookieChunk, // ;)
// sub-Meat
Chicken,
Beef,
Fish,
// sub-Fruit
Apple,
Banana,
// sub-Vegetables
Spinach,
Broccoli,
// sub-Dairy
Cheese,
Milk,
// sub-Grain
Bread,
Pasta,
Rice,
// sub-Beef
Tritip,
Hamburger,
// sub-Fish
Salmon,
Halibut,
}
注意枚举如何包含食物子类别以及子子类别。 在我的真实场景中,我有一个未知数量的顶级类别和未知数量的子级别类别(包括未知级别的子亚子类别) < / p>
我想设置枚举的位值,以便子类别(和子子类别)将“继承”其直接父级的位值。
我想避免Foods属性的客户端setter必须明确设置特定食物的所有标志。换句话说,我不希望客户端setter必须执行以下操作:
myClass.Foods = Foods.Tritip | Foods.Beef | Foods.Meat;
我正在尝试创建一个构造,以便客户端setter只需设置一个标志,并且该标志的所有父项都隐含在子标志内。
所以如果客户端setter这样做了:
myClass.Foods = Foods.Tritip;
我们将能够成功运行以下条件:
if((myClass.Foods & Foods.Meat) == Foods.Meat)
Console.WriteLine("I am meat");
if((myClass.Foods & Foods.Beef) == Foods.Beef)
Console.WriteLine("I am beef");
if((myClass.Foods & Foods.Tritip) == Foods.Tritip)
Console.WriteLine("I am tritip");
if((myClass.Foods & Foods.Meat) == Foods.Meat)
Console.WriteLine("I am meat");
if((myClass.Foods & Foods.Rice) != Foods.Rice)
Console.WriteLine("I am definitely not rice!");
让我对这个问题感到困惑的是,枚举中每个食物项目的比特值是多少。我最初开始时:
[Flags]
enum Foods
{
// Top Level foods
Meat = 0x1,
Fruit = 0x2,
Vegetables = 0x4,
Dairy = 0x8,
Grain = 0x10,
BenJerrysOatmealCookieChunk = 0x20, // ;)
...and so on.
}
..但是因为我不知道会有多少级别(枚举列表会随着时间的推移而增长)..我不知道如何创建我的位值以便它们允许增长。
以前有人试图这样做吗?如果是这样,您的位值的业务逻辑是什么?
我有一种偷偷摸摸的感觉,这个问题需要赏金;)
答案 0 :(得分:6)
你可以通过在枚举声明本身内部按位OR来做到这一点。虽然这不会给你一个很好的干净ToString,就像你通常使用Flags属性一样,它确实给你带来了理想的结果:
[Flags]
public enum Foods : ulong
{
Meat = 0x100000,
Fruit = 0x200000,
Vegetables = 0x400000,
Dairy = 0x800000,
Grain = 0xF00000,
Beef = Meat | 0x100,
Chicken = Meat | 0x200,
Fish = Meat | 0x400,
Apple = Fruit | 0x100,
Banana = Fruit | 0x200,
Spinach = Vegetables | 0x100,
Broccoli = Vegetables | 0x200,
Cheese = Dairy | 0x100,
Milk = Dairy | 0x200,
Bread = Grain | 0x100,
Pasta = Grain | 0x200,
Rice = Grain | 0x400,
Tritip = Beef | 0x1,
Hamburger = Beef | 0x2,
Salmon = Fish | 0x100,
Halibut = Fish | 0x200,
}
我使用ulong作为enum的基础,以便为子类别获得更多空间。我并没有从最高位开始,但如果我是你,我会这样做,以便有更大的空间来代表更深层次的类别。此外,您还希望利用子类别之间的间距来产生最佳结果。
答案 1 :(得分:3)
注意枚举如何包含食物子类别以及子子类别。 在我的真实场景中,我有一个未知数量的顶级类别和 未知数量的子级别类别(包括未知级别的 子分次正类别)。
然后你没有枚举:你有某种类型的层次结构。枚举(按设计)包装某种整数类型并具有固定数量的元素。它们基本上是定点整数这一事实为可用位的数量设置了上限:枚举的大小为8位,16位,32位或64位。
鉴于这些限制枚举,您可以执行以下操作(并且您不需要或不需要[Flags]
属性)。在下面的例子中,我为“类别”保留了32位,为“子类别”保留了32位。
enum Foods : ulong
{
CategoryMask = 0xFFFFFFFF00000000 ,
// Top Level foods
Meat = 0x0000000100000000 ,
Fruit = 0x0000000200000000 ,
Vegetables = 0x0000000400000000 ,
Dairy = 0x0000000800000000 ,
Grain = 0x0000001000000000 ,
BenJerrysOatmealCookieChunk = 0x0000002000000000 , // ;)
Chicken = Meat | 0x0000000000000001 ,
Beef = Meat | 0x0000000000000002 ,
Fish = Meat | 0x0000000000000004 ,
Apple = Fruit | 0x0000000000000001 ,
Banana = Fruit | 0x0000000000000002 ,
Spinach = Vegetables | 0x0000000000000001 ,
Broccoli = Vegetables | 0x0000000000000002 ,
Cheese = Dairy | 0x0000000000000001 ,
Milk = Dairy | 0x0000000000000002 ,
Bread = Grain | 0x0000000000000001 ,
Pasta = Grain | 0x0000000000000002 ,
Rice = Grain | 0x0000000000000004 ,
TriTip = Beef | 0x0000000000000001 ,
Hamburger = Beef | 0x0000000000000002 ,
Salmon = Fish | 0x0000000000000004 ,
Halibut, = Fish | 0x0000000000000008 ,
}
请注意,询问其类别的Foods
值需要稍微麻烦一点,如下所示:
Foods item = Foods.Hamburger ;
Foods category = (Foods) ( (ulong)item & (ulong)Foods.CategoryMask ) ;
答案 2 :(得分:1)
这是不可能的。枚举不能从其他枚举继承。事实上,所有枚举必须实际上从System.Enum继承。 C#允许语法更改看起来像继承的枚举值的基础表示,但实际上它们仍然继承自System.enum。
有关完整详细信息,请参阅CLI规范的第8.5.2节。来自规范的相关信息
但是,您可以使用基类和派生类实现所需的功能。