Return to Snippet

Revision: 40256
at January 28, 2011 02:50 by ronklein


Initial Code
using System;
using System.Linq;
using System.Reflection;

namespace MyEnum
{
    public class ReflectionUtils
    {
        public static string[] GetEnumValues(Type enumType)
        {
            return (from fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static) select fi.Name).ToArray();
        }
    }
}

Initial URL

                                

Initial Description
<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>

Initial Title
Get enum values by Reflection

Initial Tags

                                

Initial Language
C#