如何通过描述获取枚举值[重复]

时间:2021-12-12 22:53:23

This question already has an answer here:

这个问题在这里已有答案:

public enum States
{
        [Description("New Hampshire")]
        NewHampshire = 29,
        [Description("New York")]
        NewYork = 32,
}

Here I have to get the data by Description example: I need 29 by 'New Hampshire' Dynamically not using index position

在这里,我必须通过描述示例获取数据:我需要29个'新罕布什尔州'动态地不使用索引位置

3 个解决方案

#1


Here a generic method you can use with any attribute

这是一个可以与任何属性一起使用的通用方法

public static class Extensions
{
    public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) 
            where TAttribute : Attribute
    {
        return enumValue.GetType()
                        .GetMember(enumValue.ToString())
                        .First()
                        .GetCustomAttribute<TAttribute>();
    }
}

This will help you achieve what you're attempting...

这将帮助您实现您的目标......

#2


You can pass your string to GetDataByDescription method. I've used answer by Aydin Adn for GetAttribute method.

您可以将字符串传递给GetDataByDescription方法。我使用了Aydin Adn对GetAttribute方法的回答。

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine(GetDataByDescription("New Hampshire"));
    }

    private static int GetDataByDescription(string s)
    {
        foreach (States state in Enum.GetValues(typeof (States)))
        {
            if (GetAttribute<DescriptionAttribute>(state).Description == s)
            {
                return (int) state;
            }
        }

        throw new ArgumentException("no such state");
    }

    private static TAttribute GetAttribute<TAttribute>(Enum enumValue)
        where TAttribute : Attribute
    {
        return enumValue.GetType()
            .GetMember(enumValue.ToString())
            .First()
            .GetCustomAttribute<TAttribute>();
    }
}

#3


this is a way you can:

这是一种方式:

States s;
var type = typeof(States);
foreach (var field in type.GetFields())
{
    var attribute = Attribute.GetCustomAttribute(field,
        typeof(DescriptionAttribute)) as DescriptionAttribute;
    if (attribute != null)
    {
        if (attribute.Description == "description")
        {
            s = (States)field.GetValue(null);
            break;
        }
    }
    else
    {
        if (field.Name == "description")
        {
            s = (Rule)field.GetValue(null);
            break;
        }
    }
} 

#1


Here a generic method you can use with any attribute

这是一个可以与任何属性一起使用的通用方法

public static class Extensions
{
    public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) 
            where TAttribute : Attribute
    {
        return enumValue.GetType()
                        .GetMember(enumValue.ToString())
                        .First()
                        .GetCustomAttribute<TAttribute>();
    }
}

This will help you achieve what you're attempting...

这将帮助您实现您的目标......

#2


You can pass your string to GetDataByDescription method. I've used answer by Aydin Adn for GetAttribute method.

您可以将字符串传递给GetDataByDescription方法。我使用了Aydin Adn对GetAttribute方法的回答。

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine(GetDataByDescription("New Hampshire"));
    }

    private static int GetDataByDescription(string s)
    {
        foreach (States state in Enum.GetValues(typeof (States)))
        {
            if (GetAttribute<DescriptionAttribute>(state).Description == s)
            {
                return (int) state;
            }
        }

        throw new ArgumentException("no such state");
    }

    private static TAttribute GetAttribute<TAttribute>(Enum enumValue)
        where TAttribute : Attribute
    {
        return enumValue.GetType()
            .GetMember(enumValue.ToString())
            .First()
            .GetCustomAttribute<TAttribute>();
    }
}

#3


this is a way you can:

这是一种方式:

States s;
var type = typeof(States);
foreach (var field in type.GetFields())
{
    var attribute = Attribute.GetCustomAttribute(field,
        typeof(DescriptionAttribute)) as DescriptionAttribute;
    if (attribute != null)
    {
        if (attribute.Description == "description")
        {
            s = (States)field.GetValue(null);
            break;
        }
    }
    else
    {
        if (field.Name == "description")
        {
            s = (Rule)field.GetValue(null);
            break;
        }
    }
}