I have created an extension method as such..
public static bool AllFlagsSet<T>(this T input, params T[] values) where T : struct, IConvertible
{
bool allSet = true;
int enumVal = input.ToInt32(null);
foreach (T itm in values)
{
int val = itm.ToInt32(null);
if (!((enumVal & val) == val))
{
allSet = false;
break;
}
}
return allSet;
}
Which works great for the purposes I needed, however I now have a requirement to create a method with the same signature which checks if only exclusively those values have been set.
Basically something like this.
public static bool OnlyTheseFlagsSet<T>(this T input, params T[] values) where T : struct, IConvertible
{
}
The only way I can think of doing this is by getting all available enumeration values checking which are set and confirming that only the two provided have been.
Is there another way to solve this problem using some sort of bitwise operation?