Getting attributes of Enum's value

后端 未结 25 2579
慢半拍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:11

    Alternatively, you could do the following:

    List selectListItems = new List();
    
        foreach (var item in typeof(PaymentTerm).GetEnumValues())
        {
            var type = item.GetType();
            var name = type.GetField(item.ToString()).GetCustomAttributesData().FirstOrDefault()?.NamedArguments.FirstOrDefault().TypedValue.Value.ToString();
            selectListItems.Add(new SelectListItem(name, type.Name));
    
        }
    

提交回复
热议问题