你可以遍历所有的枚举值?
这个问题在这里已经有了答案:
如何枚举枚举? 14个答案
public enum Foos { A, B, C }  有没有办法来循环Foos的可能值? 
基本上?
 foreach(Foo in Foos) 
	
是的,您可以使用GetValues方法
 var values = Enum.GetValues(typeof(Foos)); 
或者input版本
 var values = Enum.GetValues(typeof(Foos)).Cast<Foos>(); 
我很久以前就给我的私人图书馆增加了一个帮手function
 public static class EnumUtil { public static IEnumerable<T> GetValues<T>() { return Enum.GetValues(typeof(T)).Cast<T>(); } } 
用法:
 var values = EnumUtil.GetValues<Foos>(); 
 foreach(Foos foo in Enum.GetValues(typeof(Foos))) 
 foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum))) { Console.WriteLine(val); } 
感谢Jon Skeet: http : //bytes.com/groups/net-c/266447-how-loop-each-items-enum
 foreach (Foos foo in Enum.GetValues(typeof(Foos))) { ... } 
  更新 
 有一段时间,我看到一个评论,让我回到我的旧的答案,我想我会做不同的现在。 这些天我会写: 
 private static IEnumerable<T> GetEnumValues<T>() { // Can't use type constraints on value types, so have to do check like this if (typeof(T).BaseType != typeof(Enum)) { throw new ArgumentException("T must be of type System.Enum"); } return Enum.GetValues(typeof(T)).Cast<T>(); } 
 static void Main(string[] args) { foreach (int value in Enum.GetValues(typeof(DaysOfWeek))) { Console.WriteLine(((DaysOfWeek)value).ToString()); } foreach (string value in Enum.GetNames(typeof(DaysOfWeek))) { Console.WriteLine(value); } Console.ReadLine(); } public enum DaysOfWeek { monday, tuesday, wednesday } 
  Enum.GetValues(typeof(Foos)) 
是。 在System.Enum类中使用“GetValues”方法。