Are group members listed?

I want to determine if the enum value belongs to a specific group. See the pseudo-example:

[Flags] public enum Animals { Dog = 1, Cat = 2, WildAnimal = Dog | Cat, Fly = 4, Bee = 8, Insect = Fly | Bee } public static bool IsInsect(Animals animals) { return Animals.Insect.Qualifies(animals); } public static bool Qualifies(this Animals groupName, Animals value) { //Is there a bitwise operation for it? } 
+4
source share
4 answers
+7
source
 if ((groupName & value) != 0) ... 
+2
source

Use "and" and check the common bits:

 return (groupName & value) > 0; 
0
source

Put a description attribute or a custom attribute on each individual listing, and then get this information from the reflection. I will give an example of such use with listings in my blog:

C # Using extended object attribute information

NTN

0
source

Source: https://habr.com/ru/post/1388540/


All Articles