Getting attributes of Enum's value

后端 未结 25 2542
慢半拍i
慢半拍i 2020-11-22 00:35

I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following <

25条回答
  •  醉话见心
    2020-11-22 01:12

    Alternatively, you could do the following:

    Dictionary description = new Dictionary()
        {
          { FunkyAttributesEnum.NameWithoutSpaces1, "Name With Spaces1" },
          { FunkyAttributesEnum.NameWithoutSpaces2, "Name With Spaces2" },
        };
    

    And get the description with the following:

    string s = description[FunkyAttributesEnum.NameWithoutSpaces1];
    

    In my opinion this is a more efficient way of doing what you want to accomplish, as no reflection is needed..

提交回复
热议问题