Just to add myMy solution, which works in compact framework.NET Compact Framework (3.5) and supports type checking at compile time:
public static List<T> GetEnumValues<T>() where T : new() {
T valueType = new T();
return typeof(T).GetFields()
.Select(fieldInfo => (T)fieldInfo.GetValue(valueType))
.Distinct()
.ToList();
}
public static List<String> GetEnumNames<T>() {
return typeof (T).GetFields()
.Select(info => info.Name)
.Distinct()
.ToList();
}
- If anyone knows how to get rid of the
T valueType = new T(), I'd be happy to see a solution.
A call would look like this:
List<MyEnum> result = Utils.GetEnumValues<MyEnum>();