Get Values from Enum or Use foreach in Enum

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();

        }

 

Here is an example:

public enum Months

    {

        January=1, February, March, April, May, June, July, August, September, October, November, December

    }

 

AllMonths = new ObservableCollection<string>();

 

            foreach (var item in EnumHelper.GetValues(typeof(Months)))

            {

                AllMonths.Add(Convert.ToString(item));

            }

 

Comments