C# 如何获取枚举的所有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/801029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all values of an Enum?
提问by Graviton
I want to create a method that takes in an Enum
type, and returns all it's members in an array, How to create such a function?
我想创建一个接受Enum
类型的方法,并在数组中返回它的所有成员,如何创建这样的函数?
Take for example, I have these two enums:
例如,我有这两个枚举:
public enum Family
{
Brother,
Sister,
Father
}
public enum CarType
{
Volkswagen,
Ferrari,
BMW
}
How to create a function GetEnumList
so that it returns
如何创建一个函数GetEnumList
以使其返回
{Family.Brother, Family.Sister, Family.Father}
for the first case.{CarType.Volkswagen, CarType.Ferrari, CarType.BMW}
for the second case.
{Family.Brother, Family.Sister, Family.Father}
对于第一种情况。{CarType.Volkswagen, CarType.Ferrari, CarType.BMW}
对于第二种情况。
I tried :
我试过 :
private static List<T> GetEnumList<T>()
{
var enumList = Enum.GetValues(typeof(T))
.Cast<T>().ToList();
return enumList;
}
But I got an InvalidOperationException
:
但我得到了一个InvalidOperationException
:
System.InvalidOperationException : Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true. at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException() at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
System.InvalidOperationException :无法对 ContainsGenericParameters 为 true 的类型或方法执行后期绑定操作。在 System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException() 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj) , BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfoculture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
Edit: The above code is working fine-- the reason I got an exception was because profiler caused me the bug. Thank you all for your solutions.
编辑:上面的代码工作正常——我得到异常的原因是因为探查器给我带来了这个错误。谢谢大家的解决方案。
采纳答案by rory.ap
Same as the other answer but updated for modern C#:
与其他答案相同,但针对现代 C# 进行了更新:
public static List<TEnum> GetEnumList<TEnum>() where TEnum : Enum
=> ((TEnum[])Enum.GetValues(typeof(TEnum))).ToList();
回答by Homes2001
(Family[])Enum.GetValues(typeof(Family))
回答by Homes2001
Here is the full code:
这是完整的代码:
public enum Family
{
Brother,
Sister,
Father
}
public enum CarType
{
Volkswagen,
Ferrari,
BMW
}
static void Main(string[] args)
{
Console.WriteLine(GetEnumList<Family>());
Console.WriteLine(GetEnumList<Family>().First());
Console.ReadKey();
}
private static List<T> GetEnumList<T>()
{
T[] array = (T[])Enum.GetValues(typeof(T));
List<T> list = new List<T>(array);
return list;
}
回答by JP Alioto
Like this?
像这样?
private static List<string> GetEnumList<T>()
{
return Enum.GetNames( typeof( T ) )
.Select(s => typeof(T).Name + "." + s).ToList();
}