Posted By

ronklein on 01/28/11


Tagged

enum Reflection


Versions (?)

Get enum values by Reflection


 / Published in: C#
 

This code keeps the code order. For instance:

public enum Color
{
    Red = 100,
    Green = 0,
    Blue = 5        
}

-Will generate the names as Red, Green, Blue. Using the Enum.GetNames approach won't make it.

  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4.  
  5. namespace MyEnum
  6. {
  7. public class ReflectionUtils
  8. {
  9. public static string[] GetEnumValues(Type enumType)
  10. {
  11. return (from fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static) select fi.Name).ToArray();
  12. }
  13. }
  14. }

Report this snippet  

You need to login to post a comment.