How to get all descriptions of enum values with reflection?

前端 未结 7 1467
孤独总比滥情好
孤独总比滥情好 2021-02-10 08:29

So I need to get a List from my enum

Here is what I have done so far:

enum definition

           


        
相关标签:
7条回答
  • 2021-02-10 09:16

    You can try this

    public string ContractorTypeDescription(Enum ContractorType)
    {
        FieldInfo fi = ContractorType.GetType().GetField(ContractorType.ToString());
        var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attributes.Length > 0)
        {
            return attributes[0].Description;
        }
        else
        {
            return ContractorType.ToString();
        }
    }
    
    0 讨论(0)
提交回复
热议问题