Comparing enum flags in C#

后端 未结 7 2278
一个人的身影
一个人的身影 2021-02-07 18:42

I need to detect if a flag is set within an enum value, which type is marked with the Flag attribute.

Usually it is made like that:

(value & flag) ==         


        
7条回答
  •  醉酒成梦
    2021-02-07 19:28

    I have used this to compare flags

    public static bool IsSet(this T input, T match)
    {
        return (Convert.ToUInt32(input) & Convert.ToUInt32(match)) != 0;
    }
    

    Here you can do the different conversions. From int to short to long.

提交回复
热议问题