简体   繁体   English

C#如何检查枚举值是否在参数中传递?

[英]C# How to check an enum value is passed in a parameter?

I have a method which accepts an enum as an argument: 我有一个接受枚举作为参数的方法:

[Flags]
public enum MyEnum
{
  A = 1,
  B = 2,
  C = 3
}

public class MyClass
{
  public MyEnum myEnum;
}

public bool MyMethod(MyClass class, MyEnum enumParam)
{
  // Here I want to check if object class contains some enum values passed as an argument
  // Something like: if(class.myEnum 'contains-one-of-the-items-of enumParam)
}

public void Test()
{
  Myclass class = new MyClass() { myEnum = MyEnum.A };
  MyMethod(class, MyEnum.A | MyEnum.B);
}

I want to check if an object contains one of the enum values which are passed in the method as an argument. 我想检查对象是否包含在方法中作为参数传递的枚举值之一。

作为您的使用标志,这可以帮助您检查是否已设置枚举值: [Flags]枚举属性在C#中是什么意思?

You can write it like this 你可以这样写

public bool MyMethod(MyClass class, MyEnum enumParam)
{
  if( (enumParam & MyEnum.A) != 0 ){
    ...
  }
  if( (enumParam & MyEnum.B) != 0 ){
    ...
  }
}

I changed enum to enumParam to not conflict with the enum keyword. 我将enum更改为enumParam ,以免与enum关键字冲突。

There is also a problem with your implementation since you have the values 1,2,3 for A,B,C. 由于您拥有A,B,C的值1,2,3,因此您的实现也存在问题。 This way you can't differentiate between A+B=3 and C=3. 这样,您就无法区分A + B = 3和C = 3。 A should be 1, B should be 2 and C should be 4 (D should be 8 and so on) A应该是1,B应该是2,C应该是4(D应该是8,依此类推)

EDIT 编辑

Edit due to OP's comment. 根据OP的评论进行编辑。

public bool MyMethod(MyClass class, MyEnum enumParam)
{
  return Enum.IsDefined(typeof(MyEnum), enumParam);
}

If you want to see if any of the values passed in the parameter are in the class's myEnum field, you can write: 如果要查看参数中传递的任何值是否在类的myEnum字段中,可以编写:

public bool MyMethod(MyClass class, MyEnum enum)
{
  // Here I want to check if object class contains some enum values passed as an argument
  // Something like: if(class.myEnum 'contains-one-of-the-items-of enum)
  return (this.myEnum & enum) != 0;
}

This does a logical "AND" of the bit flags and will return true if any one of the flags in enum is set in myEnum . 这将对位标志进行逻辑“与”运算,如果在myEnum设置了enum任何标志,则将返回true

If you want to ensure that all the flags are set, then you can write: 如果要确保设置了所有标志,那么可以编写:

return (this.myEnum & enum) == this.myEnum;

Also, read the response by @Øyvind Bråthen carefully. 另外,请仔细阅读@ØyvindBråthen的回复。 In order for [Flags] to work, you need to ensure that your enum values are powers of 2. 为了使[Flags]起作用,您需要确保枚举值为2的幂。

Change your enum like this : 像这样更改您的枚举:

public enum MyEnum
    {
        A = 2,
        B = 4,
        C = 8
    }

and your method is as simple as : 并且您的方法很简单:

public bool MyMethod(MyClass aClass, MyEnum aEnum)
        {
            return (aClass.myEnum & aEnum) != 0;
        }

Best regards 最好的祝福

In C# 4.0 you can easily use Enum.HasFlag method. 在C#4.0中,您可以轻松使用Enum.HasFlag方法。 You can take a look at this question to get other solutions including C# 3.5 and previous versions. 您可以看一下这个问题来获取其他解决方案,包括C#3.5和早期版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM