Get enum values by Reflection


/ Published in: C#
Save to your folder(s)

<p>This code keeps the code order. For instance:</p>

<pre><code>public enum Color
{
Red = 100,
Green = 0,
Blue = 5
}
</code></pre>

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.