.NET: Using C# generics to extract enumerated flags from a numeric value.
The CFlagsExtracter class takes two parameters: An enumeration type and an integer value (Int32). The enumeration value must be a bit field (aka set of flags). This is ensured by the presence of a FlagsAttribute
public enum EFlags
eFlag1 = 1,
eFlag2 = 2,
eFlag3 = 4,
eFlag4 = 8,
eFlag5 = 16,
eFlag6 = 32,
Once the two parameters are known, the class extracts from the integer value the flags set. For example, the decimal value 45 is equal to the binary value 101101, in which the first, third, fourth and sixth flags are set. Corresponding to the EFlags enum above, it means that eFlag1, eFlag3, eFlag4 and eFlag6 are set. In that case, the property aeFlags will contain an array of type EFlags with 4 cells, containing the corresponding enum values.
are a great addition to .NET 2.0. The are similar to C++ templates, but (in my opinion) much easier to understand and to use. They allow to define a class or a method's functionality without knowing what the types of its components (attributes, return value, parameters...) will be. As the name indicates, it allows working in generic ways, and enhance reusability of such components.
Checking if a given flag is set in an integer value is very easy when you know how: Make a logical (bitwise) AND between the integer value and the flag value. This will set all the other bits to 0. After that, compare the resulting value with the flag value, et voilà :-)
iFlagValue = (int)
iTest = iFlagValue & iValue;
if ( iTest == iFlagValue )
// The flag "eFlag2" is set in iValue
In our case, the usage of the CFlagsExtracter class is simple enough:
CFlagsExtracter<EFlags>( iValue );
foreach ( EFlags eFlag
in oExtracter.aeFlags )
// Do something
Practical example: Getting a file's attributes
The CFlagsExtracter class can be used for example to get a file or a directory's attributes. The attributes (see FileAttributes
) are an enumeration marked with the "Flags" attribute, so they are perfectly suitable for this class!
FileInfo fl = new FileInfo( @"d:\temp\test.txt" );
if ( fl.Exists )
= new CFlagsExtracter<FileAttributes>( (int) fl.Attributes );
foreach ( FileAttributes eAttribute
in oFileAttributesExtracter.aeFlags )
Console.WriteLine( eAttribute.ToString() );
The source code can be downloaded here
This demo only works on AJAX-enabled web browsers. For more information,