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