我看起来有些相同的主题,但却找不到我想要的东西 我应该使用flag enum flag atribute并检查我的数据是否在此枚举的一个集合中 例如,枚举:
[Flags]
private enum MyEnum {
Apple,
Orange,
Tomato,
Potato
Melon,
Watermelon,
Fruit = Apple | Orange,
Vegetable = Tomato | Potato,
Berry = Melon | Watermelon,
}
在方法中,我应检查输入数据。我该怎么做?
private void Checking(string data){
if(MyEnum.Fruit contains data) MessageBox.Show("Fruit");
if(MyEnum.Vegetable contains data) MessageBox.Show("Vegetables");
if(MyEnum.Berry contains data) MessageBox.Show("Berry");
}
应该是什么而不是“包含数据”?
更新
private void ZZZ(){
Cheching("Apple");
}
答案 0 :(得分:10)
首先,您需要使用powers-of-2序列手动编号您的值:
[Flags]
private enum MyEnum
{
None = 0, // often useful
Apple = 1,
Orange = 2,
Tomato = 4,
Potato = 8,
Melon = 16,
Watermelon = 32,
Fruit = Apple | Orange,
Vegetable = Tomato | Potato,
Berry = Melon | Watermelon,
}
[Flags]
属性不是绝对必要的,它只控制ToString()
行为。
要检查字符串是否与您的值匹配,您必须先将其设为枚举:
private void Checking(string data)
{
MyEnum v = (MyEnum) Enum.Parse(typeof(MyEnum), data);
if((MyEnum.Fruit & v) != 0) MessageBox.Show("It's a Fruit");
...
}
但请注意,Enum和这样的字符串与Parse()
之间的交换是有限的。
答案 1 :(得分:2)
除Henk Holterman的解决方案外,您还可以使用扩展方法:
[Flags]
private enum MyEnum {
None = 0,
Apple = 1,
Orange = 2,
Tomato = 4,
Potato = 8,
Melon = 16,
Watermelon = 32,
Berry = Melon | Watermelon,
Fruit = Apple | Orange,
Vegetable = Potato | Tomato
}
private static class MyEnumExtensions {
public static Boolean IsFruit(this MyEnum value) {
return (value & MyEnum.Fruit) == MyEnum.Fruit;
}
public static Boolean IsVegetable(this MyEnum value) {
return (value & MyEnum.Vegetable) == MyEnum.Vegetable;
}
public static Boolean IsBerry(this MyEnum value) {
return (value & MyEnum.Berry) == MyEnum.Berry;
}
}
...
MyEnum data = ...
if (data.IsBerry()) {
MessageBox.Show("Berry");
}
答案 2 :(得分:2)
您还可以使用HasFlag
- 类的Enum
- 方法。正如Henk指出的那样,需要使用powers-of-2序列的值手动为你的枚举赋值。
[Flags]
private enum MyEnum
{
Apple = 1,
Orange = 2,
Tomato = 4,
Potato = 8,
Melon 16,
Watermelon = 32,
Fruit = Apple | Orange,
Vegetable = Tomato | Potato,
Berry = Melon | Watermelon,
}
然后,要检查您是否可以使用以下方法,该方法适用于枚举的所有组成部分:
void Cheking(string data)
{
// Get the enum value of the string passed to the method
MyEnum myEnumData;
if (Enum.TryParse<MyEnum>(data, out myEnumData))
{
// If the string was a valid enum value iterate over all the value of
// the underlying enum type
var values = Enum.GetValues(typeof(MyEnum)).OfType<MyEnum>();
foreach (var value in values)
{
// If the value is not a power of 2 it is a composed one. If it furthermore
// has the flag passed to the method this is one we searched.
var isPowerOfTwo = (value != 0) && ((value & (value - 1)) == 0);
if (!isPowerOfTwo && value.HasFlag(myEnumData))
{
MessageBox.Show(value.ToString());
}
}
}
// In case an invalid value had been passed to the method
// display an error message.
else
{
MessageBox.Show("Invalid Value");
}
}
或者使用LINQ以较短的方式编写它:
var results = Enum.GetValues(typeof(MyEnum))
.OfType<MyEnum>()
.Select(x => new { Value = x, IsPowerOfTwo = (x != 0) && ((x & (x - 1)) == 0) } )
.Where(x => !x.IsPowerOfTwo && x.Value.HasFlag(myEnumData))
.Select(x => x.Value.ToString());
这将给出包含结果的IEnumerable<string>
。如果myEnumData
的值为MyEnum.Apple
,则结果将仅包含值"Fruit"
。
答案 3 :(得分:1)
MyEnum eVal= (MyEnum ) Enum.Parse( typeof(MyEnum), data, true );
if((MyEnum.Fruit & eVal) != 0) MessageBox.Show("Fruit");
您可能想要阅读有关bitwise和布尔代数的更多信息。