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) ==
I wrote a set of extension methods for enums, in case you need it :
public static class EnumExtensions
{
private static void CheckEnumWithFlags()
{
if (!typeof(T).IsEnum)
throw new ArgumentException(string.Format("Type '{0}' is not an enum", typeof(T).FullName));
if (!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
{
CheckEnumWithFlags();
long lValue = Convert.ToInt64(value);
long lFlag = Convert.ToInt64(flag);
return (lValue & lFlag) != 0;
}
public static IEnumerable GetFlags(this T value) where T : struct
{
CheckEnumWithFlags();
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
{
CheckEnumWithFlags();
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
{
CheckEnumWithFlags();
long lValue = 0;
foreach (T flag in flags)
{
long lFlag = Convert.ToInt64(flag);
lValue |= lFlag;
}
return (T)Enum.ToObject(typeof(T), lValue);
}
}
The main drawback is that you can't specify where T : Enum
: it is explicitly forbidden ("Constraint cannot be special class 'System.Enum'"), so the extension methods will appear in intellisense for all structs... I added the CheckEnumWithFlags
method to check that the type is actually an enum, and has the Flags
attribute.
UPDATE : Jon Skeet recently started an interesting library called UnconstrainedMelody which does exactly the same sort of things, and works around the generic type constraint limitation mentioned above