Loop through "selected" Enum values?
c#
Solution
How about the following:
FileStatus status = FileStatus.New|FileStatus.Amazing;
foreach (FileStatus x in Enum.GetValues(typeof(FileStatus)))
{
if (status.HasFlag(x)) Console.WriteLine("{0} set", x);
}
Or in one fell LINQ swoop:
var flags = Enum.GetValues(typeof(FileStatus))
.Cast<FileStatus>()
.Where(s => status.HasFlag(s));
Problem
I know how to loop through enum list of properties, but how would I loop through all "selected" enum properties? For example, if one did `Prop1 | Prop2` against `public enum Foo { Prop1; Prop2; Prop3 }`, how would I achieve this? This is what I have now: ``` var values = Enum.GetValues(typeof(FileStatus)).Cast<FileStatus>(); foreach (var value in values) { } ``` It loops through all enum properties, but I'd like to loop only the ones that were "selected". Update: `[Flags]` attribute was set. Update 2: The enum contains a large number of properties, I can't and won't type/hardcode a single property check, instead I want to dynamically loop through each of them and check if my enum instance `Bar` contains the looped item set.