C# Enums - Check Flags against a Mask
Asked Answered
C

3

8

I have the following enum flags:

[Flags]
private enum MemoryProtection: uint
{
    None             = 0x000,
    NoAccess         = 0x001,
    ReadOnly         = 0x002,
    ReadWrite        = 0x004,
    WriteCopy        = 0x008,
    Execute          = 0x010,
    ExecuteRead      = 0x020,
    ExecuteReadWrite = 0x040,
    ExecuteWriteCopy = 0x080,
    Guard            = 0x100,
    NoCache          = 0x200,
    WriteCombine     = 0x400,
    Readable         = (ReadOnly | ReadWrite | ExecuteRead | ExecuteReadWrite),
    Writable         = (ReadWrite | WriteCopy | ExecuteReadWrite | ExecuteWriteCopy)
}

Now i have an enum instance that I need to check if it's readable. If I use the following code:

myMemoryProtection.HasFlag(MemoryProtection.Readable)

It always returns false in my case because I think HasFlag check if it has every flag. I need something elegant to avoid doing this:

myMemoryProtection.HasFlag(MemoryProtection.ReadOnly)         ||
myMemoryProtection.HasFlag(MemoryProtection.ReadWrite)        ||
myMemoryProtection.HasFlag(MemoryProtection.ExecuteRead)      ||
myMemoryProtection.HasFlag(MemoryProtection.ExecuteReadWrite)

How can I do it?

Conduit answered 25/4, 2013 at 0:32 Comment(1)
Requires .NET 4.0 or laterRingo
G
10

You can turn the condition around, and check if the composite enum has the flag, rather than checking the flag for the composite, like this:

if (MemoryProtection.Readable.HasFlag(myMemoryProtection)) {
    ...
}

Here is an example:

MemoryProtection a = MemoryProtection.ExecuteRead;
if (MemoryProtection.Readable.HasFlag(a)) {
    Console.WriteLine("Readable");
}
if (MemoryProtection.Writable.HasFlag(a)) {
    Console.WriteLine("Writable");
}

This prints Readable.

Gelman answered 25/4, 2013 at 0:41 Comment(1)
Readers should note that HasFlag became available with .NET 4.0.M
K
4

Try bitwise operators:

[TestMethod]
public void FlagsTest()
{
    MemoryProtection mp = MemoryProtection.ReadOnly | MemoryProtection.ReadWrite | MemoryProtection.ExecuteRead | MemoryProtection.ExecuteReadWrite;
    MemoryProtection value = MemoryProtection.Readable | MemoryProtection.Writable;
    Assert.IsTrue((value & mp) == mp);
}
Knitwear answered 25/4, 2013 at 0:41 Comment(1)
This is the preferred method if you have implemented a bit-mask approach.Hers
J
4

Yes, hasFlag checks if every bit field (flag) is set.

Rather than treating Readable as a composite of all the protections that include Read in the name, can you turn the composition around? E.g.

[Flags]
private enum MemoryProtection: uint
{
    NoAccess         = 0x000,
    Read             = 0x001,
    Write            = 0x002,
    Execute          = 0x004,
    Copy             = 0x008,
    Guard            = 0x010,
    NoCache          = 0x020,
    ReadOnly         = Read,
    ReadWrite        = (Read | Write),
    WriteCopy        = (Write | Copy),
    // etc.
    NoAccess         = 0x800
}

Then you can write code like:

myMemoryProtection.HasFlag(MemoryProtection.Read)
Jdavie answered 25/4, 2013 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.