Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从枚举c获取显示名称列表#_C#_Generics_Enums_Data Annotations - Fatal编程技术网

C# 从枚举c获取显示名称列表#

C# 从枚举c获取显示名称列表#,c#,generics,enums,data-annotations,C#,Generics,Enums,Data Annotations,我正在尝试获取给定枚举的显示名称列表。如果枚举值具有显示名称,则它应位于列表中,如果没有,则默认名称应位于列表中。因此,如果我有一个枚举,如: public enum SourceFromEnum { Youtube, Reddit, Instagram, Facebook, Twitter, [Display(Name = "

我正在尝试获取给定枚举的显示名称列表。如果枚举值具有显示名称,则它应位于列表中,如果没有,则默认名称应位于列表中。因此,如果我有一个枚举,如:

 public enum SourceFromEnum
        {
            Youtube,
            Reddit,
            Instagram,
            Facebook,
            Twitter,
            [Display(Name = "News Website")]
            NewsSite,
            [Display(Name = "Phone or Computer")]
            Device,           
        }
函数生成的列表应与以下列表相同:

List enumDisplayNames=新列表()
{
“Youtube”,
“Reddit”,
“Instagram”,
“Facebook”,
“推特”,
“新闻网站”,
“电话或电脑”
};

我已经看过了,但据我所知,这些问题要么没有提供列表,要么对于我要做的事情来说过于复杂。

快速编写了一个方法,您可以从中展开

使用

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;
}

公共列表GetDisplayNames(枚举enm)
{
var type=enm.GetType();
var displaynames=新列表();
var name=Enum.GetNames(类型);
foreach(名称中的变量名称)
{
var field=type.GetField(名称);
var fds=field.GetCustomAttributes(typeof(DisplayAttribute),true);
如果(fds.Length==0)
{
displaynames.Add(名称);
}
foreach(fds中的显示属性fd)
{
displaynames.Add(fd.Name);
}
}
返回显示名;
}

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

我刚刚写了一个,如下所示:

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;
}
静态列表解析枚举(类型enumType)
{
如果(!enumType.IsEnum | | string.IsNullOrEmpty(enumType.FullName))
返回null;
var ret=新列表();
foreach(Enum.GetValues(enumType)中的var val)
{
变量定义=Enum.GetName(enumType,val);
if(string.IsNullOrEmpty(定义))
继续;
//无法使用(int)val
var代码=转换为32(val);
var description=GetDescription(枚举类型,定义);
ret.Add(描述);
}
返回ret;
}
静态字符串GetDescription(类型enumType,字符串字段)
{
FieldInfo FieldInfo=enumType.GetField(字段);
如果(fieldInfo==null)
返回字符串。空;
foreach(fieldInfo.GetCustomAttributes()中的var属性)
{
if(属性==null)
继续;
if(属性为DescriptionAttribute descAtt)
返回描述说明;
else if(attribute.ToString().IndexOf(“Display”,StringComparison.Ordinal)>0)
{
var prop=attribute.GetType().GetProperty(“名称”);
if(prop==null)
继续;
返回Convert.ToString(prop.GetValue(属性));
}
}
返回null;
}

enumDisplayNames是获取enum名称时所需的结果吗?是的,我想将
SourceFromEnum
粘贴到某个函数中,并获取与
enumDisplayNames
相同的内容。您找到的帖子会准确地告诉您需要什么。重要的部分是
GetDisplayValue()
方法(在接受的答案中)。你可以忽略所有剃须刀的东西。其他答案提供了同一主题的变体,通常都适用于您的场景。我同意这篇文章过于复杂。您可以使用LINQ在一个简单的静态方法中实现这一点。为什么不使用字典呢?