Three improvement ideas:
-Add check to ensure the
enum
is a
[Flags]
enum.
-Make the method generic and static with the
enum
type generic. I don't know how common it would be to have the type already on hand. It makes it simpler to use.
-Iterate over all of the values and bit-wise or them together to get a list of only valid bits to use for checking for invalid bits.
public static bool IsFlagsValid<T>(long value)
{
Type enumType = typeof(T);
if (enumType.IsEnum && enumType.IsDefined(typeof(FlagsAttribute), false))
{
long compositeValues = 0;
foreach (object flag in Enum.GetValues(enumType))
compositeValues |= Convert.ToInt64(flag);
return ((~compositeValues & value) == 0);
}
else
{
return false;
}
}
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.
So..If you're not moving forward, you're moving backwards.