Iterating through an enumeration in Silverlight?

silverlight

Solution

I figured out how to do this without making assumptions about the enum, mimicking the functions in .Net:

public static string[] GetNames(this Enum e) {
    List<string> enumNames = new List<string>();

    foreach (FieldInfo fi in e.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)){
        enumNames.Add(fi.Name);
    }

    return enumNames.ToArray<string>();
}

public static Array GetValues(this Enum e) {
    List<int> enumValues = new List<int>();

    foreach (FieldInfo fi in e.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)) {
        enumValues.Add((int)Enum.Parse(e.GetType(), fi.Name, false));
    }

    return enumValues.ToArray();
}

Problem

In .Net it is possible to iterate through an enumeration by using ``` System.Enum.GetNames(typeof(MyEnum)) ``` or ``` System.Enum.GetValues(typeof(MyEnum)) ``` In Silverlight 3 however, Enum.GetNames and Enum.GetValues are not defined. Does anyone know an alternative?

Original source