0

I have an enum type like this:

  public enum MyEnum: ulong
  {
    All = 0UL,
    Func1 = 1UL,
    Func2 = 4UL,
    Func3 = 8UL,
    Func4 = 16UL,
    Func5 = 32UL,
    Func6 = 256UL,
    // and so on...
    // but notice this is the order, on purpose
  }

Now, I would like to determine if it's possible, if user enters for example 9 which should be a combination of Func1 and Func3, how to get Func1 and Func3 from 9 in to the list for example. I've search this web site for answers but without luck. I need this in .net 3.5. If by some chance this is repeated question or there is similar thank you for pointing it out to me. Assume I cannot change them to be in order like 1 2 4 8 16...

Thanks

4

2 回答 2

0

您可以检查 (n & myValue) != 0 或 (n & myValue) == n

于 2013-06-14T18:25:32.953 回答
0

假设您要查询的值为myUlong,请使用以下命令:

MyEnum myValue = (MyEnum)myUlong;
var result = 
    ((MyEnum[])Enum.GetValues(typeof(MyEnum))).Where(n => (n & myValue) != 0);
//that will get you your result
于 2013-06-14T17:33:28.917 回答