How to get all descriptions of enum values with reflection?

前端 未结 7 1477
孤独总比滥情好
孤独总比滥情好 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();
        }
    }
    

提交回复
热议问题