转换一个枚举列出<串GT; [英] Convert an enum to List<string>

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

问题描述

如何转换以下枚举为字符串?

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

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



我找不到这个确切的问题,这的枚举列出是最接近的,但我特别希望名单,LT ;串>

I couldn't find this exact question, this Enum to List is the closest but I specifically want List<string>

推荐答案

使用枚举的静态方法, GetNames 。它返回一个的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();
}

这篇关于转换一个枚举列出&LT;串GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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