假设我需要在标志和非标志变体中都使用枚举。枚举的标志和非标志变体
选项1:我可以复制的一切:
enum Color { Red, Blue, Green } [Flags] enum Colors { None = 0, Red = 1, Blue = 2, Green = 4 } // use cases Color currentColor; Colors supportedColors;
选项2:我可以只使用标志变异的一切:
Colors currentColor; // ugly, since neither "None" nor "Red | Blue" should be valid
我不像以下任何一种:在选项1中,Color.Red
和Colors.Red
完全不相关,这可能需要禁止绑定代码。另外,我必须保持两个枚举同步。选项2的缺点是显而易见的。我实际上喜欢的是类似于
enum Colors = Flag set of Color;
是否有更优雅的解决方案来满足此要求?
您可以请扩展用例吗?你是否曾经需要比较'supportedColors'和'currentColor'? – Oded
@Oded:是的,能够做一些类似'bool currentColorIsSupported =(currentColor And supportedColor <> 0)'是很棒的事情。 – Heinzi