gpt4 book ai didi

c# - 使用枚举中的位置顺序创建枚举列表,而不是值

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:52 25 4
gpt4 key购买 nike

下面的代码总是按 enum 的值排序,但我想按 enum 中的位置排序。您注意到 Matchupenum 中排在第三位,但值为 6。它总是出现在列表的最后一个迭代中。

 foreach (var enumName in Enum.GetValues(enumType).Cast<TEnum>())
{

}

public enum RestrictionDetailType
{
[EnumMember(Value = "0"), Display(Name = "None")]
None = 0,
[EnumMember(Value = "1"), Display(Name = "Team")]
Team = 1,
[EnumMember(Value = "6"), Display(Name = "Matchup")]
Matchup = 6,
[EnumMember(Value = "2"), Display(Name = "Date/Time")]
DateTime = 2,
[EnumMember(Value = "3"), Display(Name = "Venue")]
VenueCourt = 3,
[EnumMember(Value = "4"), Display(Name = "Games")]
Games = 4,
[EnumMember(Value = "5"), Display(Name = "Exhibition")]
Exhibition = 5
}

最佳答案

foreach(FieldInfo fi in typeof(RestrictionDetailType).GetFields()
.Where(fi => fi.IsStatic).OrderBy(fi => fi.MetadataToken))
Console.WriteLine(fi.Name);

来源:Sort enums in declaration order

关于c# - 使用枚举中的位置顺序创建枚举列表,而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46607890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com