作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个枚举类,
public enum USERTYPE
{
Permanant=1,
Temporary=2,
}
在我的业务对象中,我只是将此枚举声明为
private List<USERTYPE> userType=new List<USERTYPE>;
在 get/set 方法中,我试过这样
public List<USERTYPE> UserType
{
get
{
return userType;
}
set
{
userType= value;
}
}
这里它返回的行数为 0,我怎样才能在这里获取 Enum 中的所有值,有人能帮我吗...
最佳答案
您可以使用它来获取所有单独的枚举值:
private List<USERTYPE> userTypes = Enum.GetValues(typeof(USERTYPE)).Cast<USERTYPE>().ToList();
如果你经常做这样的事情,你可以为它创建一个通用的实用方法:
public static T[] GetEnumValues<T>() where T : struct {
if (!typeof(T).IsEnum) {
throw new ArgumentException("GetValues<T> can only be called for types derived from System.Enum", "T");
}
return (T[])Enum.GetValues(typeof(T));
}
关于c# - 列表中的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11240236/
我是一名优秀的程序员,十分优秀!