Convert an enum to List

后端 未结 2 1340
自闭症患者
自闭症患者 2020-12-04 20:41

How do I convert the following Enum to a List of strings?

[Flags]
public enum DataSourceTypes
{
    None = 0,
    Grid = 1,
    ExcelFile = 2,
    ODBC = 4
}         


        
2条回答
  •  悲哀的现实
    2020-12-04 20:59

    Use Enum's static method, GetNames. It returns a string[], like so:

    Enum.GetNames(typeof(DataSourceTypes))
    

    If you want to create a method that does only this for only one type of enum, and also converts that array to a List, you can write something like this:

    public List GetDataSourceTypes()
    {
        return Enum.GetNames(typeof(DataSourceTypes)).ToList();
    }
    

    You will need Using System.Linq; at the top of your class to use .ToList()

提交回复
热议问题