列表中的枚举值 [英] Enum values in a list

查看:74
本文介绍了列表中的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举类,

public enum USERTYPE
{
   Permanant=1,
   Temporary=2,
}

在我的业务对象中,我只是将该枚举声明为

in my business object I just declare this enum as

private List<USERTYPE> userType=new List<USERTYPE>;

在get/set方法中,我尝试了

and in the get/set method, I tried like

public List<USERTYPE> UserType
    {
        get 
        {
            return userType;
        }
        set
        { 
            userType= value; 
        }
    }

在这里它返回的行数为0,我如何在这里获取枚举中的所有值,有人可以在这里帮助我...

here it returns the no of rows as 0, how can I get all the values in the Enum here, can anyone help me here...

推荐答案

您可以使用它来获取所有单个枚举值:

You can use this to get all individual enum values:

private List<USERTYPE> userTypes = Enum.GetValues(typeof(USERTYPE)).Cast<USERTYPE>().ToList();

如果您经常执行此类操作,则可以为其创建通用的实用程序方法:

If you do things like this more often, you could create a generic utility method for it:

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));
}

这篇关于列表中的枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆