Possible Duplicate:
Iterating through an enumeration in Silverlight?
I came up with a handy little function that gets me the count of an enum (I know it won't work properly with all enums). Rather than hard coding the Enum into the function such that I have to write a seprate function for each Enum I want to use it with, I wanted to pass the enum in as an argument but I am having difficulty figuring out how to do this.
Here is the code:
private enum MyColors { Red, Green, Blue }
private Int32 GetEnumCount()
{
Int32 i = 0;
while (Enum.IsDefined(typeof(MyColors), (MyColors)i))
{
i++;
}
return i;
}
UPDATE
I came up with this as the answer in the end:
private Int32 GetEnumCount(Type enumType)
{
Int32 i = 0;
while (Enum.IsDefined(enumType, i))
{
i++;
}
return i;
}