Consider the following enum:
enum Color
{
None = 0,
Yellow = 1,
Green = 2,
Blue = 4
}
var enumType = typeof(Color);
var number = (int)(Color.Yellow | Color.Green);
// number equals to 3
Now I want the reverse of the above operation. I wanna know what enumerations lead to the number 3. like:
string[] names = GetEnumNames(enumType, 3);
// It should return an array { "Yellow", "Green" }
I have tried Enum.ToObject
and I know that it returns the Enum with "Yellow"
and "Green"
in it. But how can I get the list of names?
Note: Just to mention, I only know the type of the Enum at runtime.