相关疑难解决方法(0)

如何使用自定义订单属性对枚举进行排序?

我有这样的枚举:

enum MyEnum{
 [Order(1)]
 ElementA = 1,
 [Order(0)]
 ElementB = 2,
 [Order(2)]
 ElementC = 3
}
Run Code Online (Sandbox Code Playgroud)

我想列出按我编写的自定义顺序属性排序的元素,以便我得到一个排序的项目列表.

我得到描述属性,但只是这样的一个元素:

FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
Run Code Online (Sandbox Code Playgroud)

它可能是相同的但需要在所有枚举上工作并返回列表或其他排序的枚举.

c# enums

11
推荐指数
2
解决办法
5398
查看次数

标签 统计

c# ×1

enums ×1