如何使用反射来获取枚举值的所有描述? [英] How to get all descriptions of enum values with reflection?

查看:167
本文介绍了如何使用反射来获取枚举值的所有描述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要从我的枚举中获得一个列表< string>

So I need to get a List<string> from my enum

这是我迄今为止所做的:

Here is what I have done so far:

枚举定义

    [Flags]
    public enum ContractorType
    {
        [Description("Recipient")]
        RECIPIENT = 1,
        [Description("Deliver")]
        DELIVER = 2,
        [Description("Recipient / Deliver")]
        RECIPIENT_DELIVER = 4
    }

HelperClass采用我所需要的方法:

public static class EnumUtils
{
    public static IEnumerable<string> GetDescrptions(Type enumerator)
    {
        FieldInfo[] fi = enumerator.GetFields();

        List<DescriptionAttribute> attributes = new List<DescriptionAttribute>();
        foreach (var i in fi)
        {
            try
            {
                yield return attributes.Add(((DescriptionAttribute[])i.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false))[0]);
            }
            catch { }
        }
        return new List<string>{"empty"};
    }
}

现在我在 yield 值,我得到一个 NullReferenceException 。我错过了什么?语法看起来对我有用,但也许我忽略了一些东西?

Now in the line where I yield values, I got a NullReferenceException. Did I miss something? The syntax looks all right to me, but maybe I overlooked something?

编辑:
我使用的是.NET Framework 4.0

I'm using .net Framework 4.0 here.

推荐答案

这是一个小的可重用的解决方案。这是一个抽象类,它将从 T 类型中提取 K 类型的所有属性。

Here is a small reusable solution. This is an abstract class which will extract all the attributes of type K from type T.

abstract class AbstractAttributes<T, K>
{
    protected List<K> Attributes = new List<K>();

    public AbstractAttributes()
    {
        foreach (var member in typeof(T).GetMembers())
        {
            foreach (K attribute in member.GetCustomAttributes(typeof(K), true)) 
                Attributes.Add(attribute);                
        }
    }
}

我们现在要提取只有 DescriptionAttribute 类型的属性,我们将使用以下类。

Should we now want to extract only attributes of DescriptionAttribute type, we would use the following class.

class DescriptionAttributes<T> : AbstractAttributes<T, DescriptionAttribute>
{
    public List<string> Descriptions { get; set; }

    public DescriptionAttributes()
    {
        Descriptions = Attributes.Select(x => x.Description).ToList();
    }
}

此类将仅提取 DescriptionAttribute 从类型 T 键入。但是要实际上在你上下文中使用这个类,你只需要执行以下操作。

This class will extract only attributes of DescriptionAttribute type from the type T. But to actually use this class in you context you will simply need to do the following.

new DescriptionAttributes<ContractorType>().Descriptions.ForEach(x => Console.WriteLine(x));

这一行代码将写出您所使用的所有描述,您的属性类型为 DescriptionAttribute 。如果您需要提取一些其他属性,只需创建一个派生自 AbstractAttributes< T,K> 类的新类,并关闭其类型 K 具有相应的属性。

This line of code will write out all the descriptions you used as parameters in your attributes of type DescriptionAttribute. Should you need to extract some other attributes, just create a new class that derives from the AbstractAttributes<T, K> class and close its type K with the appropriate attribute.

这篇关于如何使用反射来获取枚举值的所有描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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