General method for converting an enumeration to a <T> list

Here is the "weird" question:

Is it possible to create a method in which it converts all the enumerations into a list. Here is my project of what I am thinking right now.

public class EnumTypes
{
   public enum Enum1
   {
      Enum1_Choice1 = 1,
      Enum1_Choice2 = 2
   }

   public enum Enum2
   {
      Enum2_Choice1 = 1,
      Enum2_Choice2 = 2
   }

   public List<string> ExportEnumToList(<enum choice> enumName)
   {
      List<string> enumList = new List<string>();
      //TODO: Do something here which I don't know how to do it.
      return enumList;
   }
}

Just wondering if this is possible and how to do it.

+5
source share
2 answers
Enum.GetNames( typeof(EnumType) ).ToList()

http://msdn.microsoft.com/en-us/library/system.enum.getnames.aspx

Or, if you want a fantasy:

    public static List<string> GetEnumList<T>()
    {
        // validate that T is in fact an enum
        if (!typeof(T).IsEnum)
        {
            throw new InvalidOperationException();
        }

        return Enum.GetNames(typeof(T)).ToList();
    }

    // usage:
    var list = GetEnumList<EnumType>();
+11
source
public List<string> ExportEnumToList(<enum choice> enumName)    {
List<string> enumList = new List<string>();
//TODO: Do something here which I don't know how to do it.
foreach (YourEnum item in Enum.GetValues(typeof(YourEnum ))){
    enumList.Add(item);
}
return enumList;    

}

0
source

All Articles