Considering this:
[Flags]
public enum MyEnum {
One = 1,
Two = 2,
Four = 4,
Eight = 8
}
public static class FlagsHelper
{
public static bool
You can basically use your existing extension method, byt use the Enum
type instead of MyEnum
. The problem then is that it doesn't know the enums are flags and won't allow the &
operator, so you just have to convert the enum values to numbers.
public static bool Contains(this Enum keys, Enum flag)
{
if (keys.GetType() != flag.GetType())
throw new ArgumentException("Type Mismatch");
return (Convert.ToUInt64(keys) & Convert.ToUInt64(flag)) != 0;
}
And a unit test for good measure:
[TestMethod]
public void TestContains()
{
var e1 = MyEnum.One | MyEnum.Two;
Assert.IsTrue( e1.Contains(MyEnum.Two) );
var e2 = MyEnum.One | MyEnum.Four;
Assert.IsFalse(e2.Contains(MyEnum.Two));
}