Search code examples
c#visual-studio-2015enumsenum-flags

Using C# Enum Flags To Return A Set Of Values Based On Their Bit-Wise OR'ed Values


I understand that you can configure C# enum flags in this way:

    [Flags]
    public enum MyEnum
    {
        Unknown = 0,
        Type1 = 1,
        Type2 = 2,
        Type3 = 4,
        Type4 = 8,
        Type5 = 16
    }

And once this is configured, you can represent a set of enumberations like so:

MyEnum enumerationSet = MyEnum.Type1 | MyEnum.Type2

Then you can make checks against this set, such as:

if(enumerationSet.HasFlag(MyEnum.Type1))
{
  // Do something
}

Or print their values, like so:

Console.WriteLine("{0}", enumerationSet);

Which would print:

Type1, Type2

However, can I go in reverse order? For instance, if I know that

MyEnum.Type1 | MyEnum.Type2 == 3

Can I then ask MyEnum what set of its value/types would equal 3? Then, I can create an extension method (GetSet) to execute like this:

List<MyEnum> myEnumSetList = MyEnum.GetSet(3)

Returning either a MyEnum set or a set of values, i.e. {1, 2}.

Please advise.


EDIT: I was able to finally figure it out. Posted my answer below.


Solution

  • After some hacking around, I was able to resolve a list of enum values, based on the value of that respective set OR'ed:

    protected List<MyEnum> GetEnumSet(int flagNumber)
    {
          List<MyEnum> resultList= new List<MyEnum>();
    
          foreach (MyEnum value in Enum.GetValues(typeof(MyEnum)))
          {
             if (((MyEnum)flagNumber).HasFlag(value))
             {
                 resultList.Add(value);
             }
          }
    
          return resultList;
    }
    

    Here, flagNumber is the value of a respective list OR'ed, i.e. MyEnum.Type1 | MyEnum.Type2. Thus, by sending this method the flagNumber, I get the list { MyEnum.Type1, MyEnum.Type2 }