Is it possible to prevent random bitwise operators on a field without a bitmask?

In C #, you are advised to add the [Flags] attribute to bitmask enumerations, for example:

[Flags]
public enum Condiments
{
    None = 0,
    Ketchup = 1,
    Mustard = 2,
    Mayo = 4,
    Pickle = 8,
    AllTheWay = 15
}

I found that I had code that mistakenly performed bitwise operations on an enumeration without an attribute [Flags]that was not a bitmask at all (First = 1, Second = 2, Third = 3, etc.). This was, of course, logically wrong, but quite acceptable for the compiler.

I am wondering if there is a way to use an attribute [Flags]or some other approach to turn this into a compilation error / warning. I don't know where to start, but it seems like this should be doable, so any help would be appreciated.

+5
1
+1

All Articles