How do I determine if an Enum value has one or more of the values it's being compared with?

后端 未结 8 1544
死守一世寂寞
死守一世寂寞 2021-01-04 07:53

I\'ve got an Enum marked with the [Flags] attribute as follows:

[Flags]
public enum Tag : int
{
    None = 0,
    PrimaryNav = 1,
    HideChildPages = 2,
            


        
相关标签:
8条回答
  • 2021-01-04 08:23

    You could use Jon Skeet's Unconstrained Melody library:

    var someEnumValue = Tag.PrimaryNav | Tag.HideChildPages;
    someEnumValue.HasAny(Tag.PrimaryNav | Tag.HomePage); // Returns true
    
    0 讨论(0)
  • 2021-01-04 08:26
    var tag = Tag.HideChildPages | Tag.PrimaryNav;
    
    If ((tag & Tag.PrimaryNav) == Tag.PrimaryNav) {
        // Tag.PrimaryNav set.
    }
    
    0 讨论(0)
提交回复
热议问题