【问题标题】:Get list of display names from enum c# [duplicate]从枚举c#中获取显示名称列表[重复]
【发布时间】:2020-10-10 02:11:29
【问题描述】:

我正在尝试获取给定枚举的显示名称列表。如果枚举值有显示名称,它应该在列表中,如果没有,则默认名称应该在列表中。所以如果我有一个像这样的枚举:

 public enum SourceFromEnum
        {
            Youtube,
            Reddit,
            Instagram,
            Facebook,
            Twitter,
            [Display(Name = "News Website")]
            NewsSite,
            [Display(Name = "Phone or Computer")]
            Device,           
        }

我的函数生成的列表应该与:

  List<string> enumDisplayNames = new List<string>()
            {
            "Youtube",
            "Reddit",
            "Instagram",
            "Facebook",
            "Twitter",
            "News Website",
            "Phone or Computer"
            };

我查看了this post,但据我所知,这些问题要么没有提供列表,要么对于我正在尝试做的事情过于复杂。

【问题讨论】:

  • enumDisplayNames 是获取枚举名称时想要的结果吗?
  • 是的,我想将SourceFromEnum 粘贴到某个函数中,然后得到与enumDisplayNames 相同的东西
  • 您找到的帖子确切地告诉您您需要什么。重要的部分是 GetDisplayValue() 方法(在接受的答案中)。你可以忽略所有 Razor 的东西。其他答案提供了同一主题的变体,并且通常都适用于您的场景。
  • 我同意帖子过于复杂。您可以使用 LINQ 在简单的静态方法中执行此操作。
  • 你为什么不用字典?

标签: c# generics enums data-annotations


【解决方案1】:

快速写了一个方法,你可以从那里扩展。

使用

SourceFromEnum test = new SourceFromEnum();
    
    var me =GetDisplayNames(test);

方法

public  List<string> GetDisplayNames(Enum enm)
{
    var type=enm.GetType();
    var displaynames = new List<string>();
    var names = Enum.GetNames(type);
    foreach (var name in names)
    {
        var field = type.GetField(name);
        var fds = field.GetCustomAttributes(typeof(DisplayAttribute), true);
        
        if (fds.Length==0)
        {
            displaynames.Add(name);
        }
        
        foreach (DisplayAttribute fd in fds)
        {
            displaynames.Add(fd.Name);
        }
    }
    return displaynames;
}

可以使其成为静态、错误检查等

【讨论】:

    【解决方案2】:

    我刚刚写了一个,像这样:

    static List<String> ParseEnums(Type enumType)
    {
        if (!enumType.IsEnum || string.IsNullOrEmpty(enumType.FullName))
            return null;
    
        var ret = new List<string>();
        foreach (var val in Enum.GetValues(enumType))
        {
            var definition = Enum.GetName(enumType, val);
            if (string.IsNullOrEmpty(definition))
                continue;
    
            // can't use (int)val 
            var code = Convert.ToInt32(val);
            var description = GetDescription(enumType, definition);
    
            ret.Add(description);
        }
    
        return ret;
    }
    
    static string GetDescription(Type enumType, string field)
    {
        FieldInfo fieldInfo = enumType.GetField(field);
        if (fieldInfo == null)
            return string.Empty;
    
        foreach (var attribute in fieldInfo.GetCustomAttributes())
        {
            if (attribute == null)
                continue;
            if (attribute is DescriptionAttribute descAtt)
                return descAtt.Description;
            else if (attribute.ToString().IndexOf("Display", StringComparison.Ordinal) > 0)
            {
                var prop = attribute.GetType().GetProperty("Name");
                if (prop == null)
                    continue;
                return Convert.ToString(prop.GetValue(attribute));
            }
        }
    
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2020-11-04
      • 1970-01-01
      • 2013-09-15
      • 2017-04-21
      相关资源
      最近更新 更多