I know in .NET 4 you can use HasFlag
Is there any alternative to the following in .NET 3.5?
if ((enumVar & EnumType.ValueOne) == EnumType.ValueOne)
{
// someMethod(1) or someMethod(EnumType.ValueOne)
}
if ((enumVar & EnumType.ValueTwo) == EnumType.ValueTwo)
{
// someMethod(2) or someMethod(EnumType.ValueTwo)
}
if ((enumVar & EnumType.ValueThree) == EnumType.ValueThree)
{
// someMethod(3) or someMethod(EnumType.ValueThree)
}
if ((enumVar & EnumType.ValueFour) == EnumType.ValueFour)
{
// someMethod(4) or someMethod(EnumType.ValueFour)
}
…etc for each value in the enum? You must be able to use a for..each loop to accomplish this where the argument to someMethod is the index of the loop?
[Flags]
enum EnumType
{
ValueOne = 1
, ValueTwo = 2
, ValueThree = 4
, ValueFour = 8
}
EDIT: Only worth looking at the accepted answer, the rest of the comments/answers can be safely ignored.
You should be able to write something like this. You can make it generic if you want, but there’s no way to set the constraint to be enum, so you’d have to check that yourself with reflection.