How do you iterate over Flag<Permission>? #5
-
For Enums we can do the following: foreach (var item in Enum.GetNames(typeof(MyEnum)))
{
//Do something with the item
} How would you iterate over the // not sure what to do here to make features a List<string>, ReadOnlyCollection<string>, etc.?
var allFeatureNames = (new InfiniteEnumFlags.Flag<Feature>()).[something missing here];
foreach (string featureName in allFeatureNames )
{
if(item != "None")
{
//Do something with the featureName
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Well, you're right. I'll add this feature in the next versions, but for now, you can use reflection to access names. this is used in the Example project to GetAll permissions with the values. public static Dictionary<string,Flag<Permission>> GetAll()
{
return typeof(Permission)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(f => f.FieldType == typeof(Flag<Permission>))
.ToDictionary(f => f.Name, f => (Flag<Permission>) f.GetValue(null)!);
} |
Beta Was this translation helpful? Give feedback.
-
These functionalities were added to InfiniteEnum classes in EnumClassName.GetKeyValues() |
Beta Was this translation helpful? Give feedback.
These functionalities were added to InfiniteEnum classes in
v0.4.1
.EnumClassName.GetKeyValues()
EnumClassName.GetNames()