【问题标题】:Convert an enum to List<string>将枚举转换为 List<string>
【发布时间】:2013-02-20 03:09:52
【问题描述】:

如何将以下 Enum 转换为字符串列表?

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

我找不到这个确切的问题,这个Enum to List 是最接近的,但我特别想要List&lt;string&gt;

【问题讨论】:

    标签: c# .net enums generic-list


    【解决方案1】:

    使用Enum的静态方法GetNames。它返回一个string[],如下所示:

    Enum.GetNames(typeof(DataSourceTypes))
    

    如果您想创建一个仅针对一种类型的enum 执行此操作的方法,并将该数组转换为List,您可以编写如下内容:

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

    您需要在班级顶部使用Using System.Linq; 才能使用 .ToList()

    【讨论】:

    • @DCShannon 请不要编辑热门问题/答案和缩小解释。虽然你和我都了解速记代码,但新手需要所有额外的细节来它与他们的学习联系起来。
    • 似乎Enum.GetNames(typeof(DataSourceTypes)) 返回一个通用的System.Array 而不是一个字符串数组?
    • @sookie,查看msdn链接,这是GetNames()方法的签名:public static string[] GetNames
    【解决方案2】:

    我想添加另一个解决方案: 就我而言,我需要在下拉按钮列表项中使用 Enum 组。所以他们可能有空间,即需要更多用户友好的描述:

      public enum CancelReasonsEnum
    {
        [Description("In rush")]
        InRush,
        [Description("Need more coffee")]
        NeedMoreCoffee,
        [Description("Call me back in 5 minutes!")]
        In5Minutes
    }
    

    在一个辅助类(HelperMethods)中,我创建了以下方法:

     public static List<string> GetListOfDescription<T>() where T : struct
        {
            Type t = typeof(T);
            return !t.IsEnum ? null : Enum.GetValues(t).Cast<Enum>().Select(x => x.GetDescription()).ToList();
        }
    

    当您调用此助手时,您将获得项目描述列表。

     List<string> items = HelperMethods.GetListOfDescription<CancelReasonEnum>();
    

    补充: 无论如何,如果你想实现这个方法,你需要枚举的 :GetDescription 扩展。这是我用的。

     public static string GetDescription(this Enum value)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    DescriptionAttribute attr =Attribute.GetCustomAttribute(field,typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return null;
            /* how to use
                MyEnum x = MyEnum.NeedMoreCoffee;
                string description = x.GetDescription();
            */
    
        }
    

    【讨论】:

    • 而不是[Description( 使用[EnumMember(Value = 自动反序列化 JSON 枚举!它易于实现,更改:x =&gt; x.GetDescription()x =&gt; x.GetAttributeOfType&lt;EnumMemberAttribute&gt;().Value 和方法:public static T GetAttributeOfType&lt;T&gt;(this Enum enumVal) where T : System.Attribute { var type = enumVal.GetType(); var memInfo = type.GetMember(enumVal.ToString()); var attributes = memInfo[0].GetCustomAttributes(typeof(T), false); return (attributes.Length &gt; 0) ? (T)attributes[0] : null; }
    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多