C#, Flags Enum, Generic function to look for a flag

后端 未结 11 1577
陌清茗
陌清茗 2020-12-30 05:43

I\'d like one general purpose function that could be used with any Flags style enum to see if a flag exists.

This doesn\'t compile, but if anyone has a suggestion, I

11条回答
  •  情歌与酒
    2020-12-30 06:17

    Well, I don't believe there is a way to do this, as there are no constraints that apply to bitwise operators.

    However... you can just cast your enum to int and do it.

    public static Boolean IsEnumFlagPresent(int value,int lookingForFlag) 
    {
        return ((value & lookingForFlag) == lookingForFlag);
    }
    

    This works, but may be confusing to someone.

提交回复
热议问题