一段代码抵千字...
public enum enTest { a, b, c }
public void PrintEnum<T>()
{
foreach (var E in Enum.GetValues(typeof(T)))
Debug.WriteLine(E.ToString());
}
PrintEnum<enTest>();
PrintEnum<enTest?>(); // This will cause failure in Enum.GetValues()
上面是从一个更大的问题简化来说明失败的。
有谁知道当有人向我传递 Nullable Enum 时我如何遍历(或获取其中的所有值)?
提前致谢。
最佳答案
这个怎么样?
public static void PrintEnum<T>()
{
Type t = typeof (T);
if (t.IsGenericType)
{
//Assume it's a nullable enum
t = typeof (T).GenericTypeArguments[0];
}
foreach (var E in Enum.GetValues(t))
Console.WriteLine(E.ToString());
}
关于c# - CSharp 通过可空枚举进行枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31070654/