How to check if any flags of a flag combination are set?

前端 未结 16 691
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 17:24

Let\'s say I have this enum:

[Flags]
enum Letters
{
     A = 1,
     B = 2,
     C = 4,
     AB = A | B,
     All = A | B | C,
}

To check i

16条回答
  •  囚心锁ツ
    2020-11-29 17:38

    I use extension methods to write things like that :

    if (letter.IsFlagSet(Letter.AB))
        ...
    

    Here's the code :

    public static class EnumExtensions
    {
        private static void CheckIsEnum(bool withFlags)
        {
            if (!typeof(T).IsEnum)
                throw new ArgumentException(string.Format("Type '{0}' is not an enum", typeof(T).FullName));
            if (withFlags && !Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
                throw new ArgumentException(string.Format("Type '{0}' doesn't have the 'Flags' attribute", typeof(T).FullName));
        }
    
        public static bool IsFlagSet(this T value, T flag) where T : struct
        {
            CheckIsEnum(true);
            long lValue = Convert.ToInt64(value);
            long lFlag = Convert.ToInt64(flag);
            return (lValue & lFlag) != 0;
        }
    
        public static IEnumerable GetFlags(this T value) where T : struct
        {
            CheckIsEnum(true);
            foreach (T flag in Enum.GetValues(typeof(T)).Cast())
            {
                if (value.IsFlagSet(flag))
                    yield return flag;
            }
        }
    
        public static T SetFlags(this T value, T flags, bool on) where T : struct
        {
            CheckIsEnum(true);
            long lValue = Convert.ToInt64(value);
            long lFlag = Convert.ToInt64(flags);
            if (on)
            {
                lValue |= lFlag;
            }
            else
            {
                lValue &= (~lFlag);
            }
            return (T)Enum.ToObject(typeof(T), lValue);
        }
    
        public static T SetFlags(this T value, T flags) where T : struct
        {
            return value.SetFlags(flags, true);
        }
    
        public static T ClearFlags(this T value, T flags) where T : struct
        {
            return value.SetFlags(flags, false);
        }
    
        public static T CombineFlags(this IEnumerable flags) where T : struct
        {
            CheckIsEnum(true);
            long lValue = 0;
            foreach (T flag in flags)
            {
                long lFlag = Convert.ToInt64(flag);
                lValue |= lFlag;
            }
            return (T)Enum.ToObject(typeof(T), lValue);
        }
    
        public static string GetDescription(this T value) where T : struct
        {
            CheckIsEnum(false);
            string name = Enum.GetName(typeof(T), value);
            if (name != null)
            {
                FieldInfo field = typeof(T).GetField(name);
                if (field != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return null;
        }
    }
    

提交回复
热议问题