I've search for a solution for my problem but so far i was not able to figure out how to do it.
I need to get a list of values of an enum
flagged by a int
value.
I was able to do it in a specific case, but I want to create a generic function for a generic enumerator.
The code for the specicic case is:
IList<MyEnum> EnumList= new List<MyEnum>();
MyEnum EnumIUseToFlag= (MyEnum)intToParse;
foreach (MyEnumitem in Enum.GetValues(typeof(MyEnum)))
{
if (EnumIUseToFlag.HasFlag(item))
{
EnumList.Add(item);
}
}
Now, for the generic method i was trying something like:
public static IList<T> GetFlaggedValues<T>(int intValue) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}
IList<T> listToReturn = new List<T>();
Enum enumToParse = Enum.Parse(typeof(T), intValue.ToString()) as Enum;
foreach (T item in Enum.GetValues(typeof(T)))
{
// here I am not able to cast item in the integer
//values i need to use in order to flag my enum
}
return listToReturn;
}