Reputation: 16502
Silverlight is missing the GetValues for enums, so i thought i would write an extension method to cover my needs in my project. Only thing is, Im not sure what the signature of the extension method should look like. Im thinking something along the lines of:
public static IEnumerable<Enum> GetValues(this Enum e)
But its not showing up in intellisense, so i know im wrong. Any pointers?
Upvotes: 3
Views: 1417
Reputation: 16502
I think i figured it out by combining a little reflection with and digging in Reflector:
public static Array GetValues(this Enum enumType)
{
Type type = enumType.GetType();
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
Array array = Array.CreateInstance(type, fields.Length);
for (int i = 0; i < fields.Length; i++)
{
var obj = fields[i].GetValue(null);
array.SetValue(obj, i);
}
return array;
}
Upvotes: 5