Post

Missing Enum.GetValues() when doing Silverlight for instance ?

In some implementations of the .net framework, convience methods such as the static method GetValues() on the Enum class has been stripped away. This turned out to be true in the Silverlight subset of the framework. A method I’ve grown attached to over the years..   

There is a way of getting the values from an Enum through reflection; by using the GetFields() method on the type of the enum you wish to get the values from.

The GetFields() method returns an array of FieldInfo. An enum will have an array of all it’s values represented as fields. In addition it will return elements that are not values on the enum. Within FieldInfo you’ll find a set of Is*() methods, one of them are very interesting in this case; IsLiteral. All values are marked as true. The solution is then pure and simple (C# 3.0 syntax, requires LINQ) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public static class EnumHelper
{
    public static T[] GetValues<T>()
    {
        Type enumType = typeof(T);

        if (!enumType.IsEnum)
        {
            throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");
        }

        List<T> values = new List<T>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;

        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            values.Add((T)value);
        }

        return values.ToArray();
    }

    public static object[] GetValues(Type enumType)
    {
        if (!enumType.IsEnum)
        {
            throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");
        }

        List<object> values = new List<object>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;

        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            values.Add(value);
        }

        return values.ToArray();
    }

}

This post is licensed under CC BY 4.0 by the author.