将枚举转换为 List<string> [英] Convert an enum to List<string>
本文介绍了将枚举转换为 List<string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将以下枚举转换为字符串列表?
How do I convert the following Enum to a List of strings?
[Flags]
public enum DataSourceTypes
{
None = 0,
Grid = 1,
ExcelFile = 2,
ODBC = 4
};
我找不到这个确切的问题,这个 Enum to List 是最接近的,但我特别想要 List
I couldn't find this exact question, this Enum to List is the closest but I specifically want List<string>
推荐答案
使用 Enum
的静态方法,GetNames
.它返回一个 string[]
,如下所示:
Use Enum
's static method, GetNames
. It returns a string[]
, like so:
Enum.GetNames(typeof(DataSourceTypes))
如果你想创建一个只对一种类型的 enum
执行此操作的方法,并将该数组转换为 List
,你可以这样写:
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<string> GetDataSourceTypes()
{
return Enum.GetNames(typeof(DataSourceTypes)).ToList();
}
您将需要在类的顶部 Using System.Linq;
使用 .ToList()
You will need Using System.Linq;
at the top of your class to use .ToList()
这篇关于将枚举转换为 List<string>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文