的考虑下面的枚举:优先枚举名
[Flags]
public enum Intervals
{
Root = PerfectUnison,
Unison = PerfectUnison,
PerfectUnison = 1 << 0,
AugmentedUnison = MinorSecond,
MinorSecond = 1 << 1,
Second = MajorSecond,
MajorSecond = 1 << 2,
AugmentedSecond = MinorThird,
MinorThird = 1 << 3,
Third = MajorThird,
MajorThird = 1 << 4,
AugmentedThird = PerfectFourth,
DoubleAugmentedThird = Triton,
DiminishedFourth = MajorThird,
Fourth = PerfectFourth,
PerfectFourth = 1 << 5,
AugmentedFourth = Triton,
DoubleAugmentedFourth = PerfectFifth,
Triton = 1 << 6,
//...Removed for brevity, see link to code bellow
}
我想这个简单的测试:
static void Main(string[] args)
{
var values = Enum.GetValues(typeof(Intervals));
foreach (var value in values)
{
Console.WriteLine(value);
}
}
这里是输出:
PerfectUnison,PerfectUnison, PerfectUnison,AugmentedUnison,AugmentedUnison,Second,Second,MinorThird,MinorThird,DiminishedFourth,DiminishedFourth,DiminishedFourth,AugmentedThird,AugmentedThi RD,AugmentedThird,AugmentedThird,DoubleDiminishedSixth,DoubleDiminishedSixth等
虽然我想选择用于相同的值枚举名称为以下序列组成:
根,MinorSecond,第二,MinorThird,第三,四,海卫,五,MinorSixth,第六,MinorSeventh,七,八度,MinorNinth,第九,第十,第十一,MajorEleventh,十三
一个很好的再现也将是Enum.GetNames
。我希望上述组的名称应该总是在它们的值匹配名称之前。
我基本上是在寻找每个值的枚举名称优先级/优先级规则的文档。
你可以在这里使用代码:http://rextester.com/EJOWK87857。
更新
我现在正在研究反编译Enum.GetNames
。看起来它使用反射。那么问题是,“如何控制反射场的顺序?”。
AFAIK,这是不雷尔可能。例如,[Enum.GetName](http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx)方法明确提到您不应该依赖于检索名称的一致结果具有重复值的枚举。您可以使用'Enum.GetNames'(它会根据需要返回值),然后使用这些名称依次使用[Enum.Parse](http://msdn.microsoft.com/en-us /library/essfb559%28v=vs.90%29.aspx)method.EDIT:但是'Parse'的结果仍然是重复的,而不是原件;不知道这是否有帮助。 – 2013-05-12 02:25:11
@ChrisSinclair我现在正在研究反编译的'Enum.GetNames'。看起来它使用反射。那么问题是,“如何控制反射场的顺序?”。 – Shimmy 2013-05-12 02:48:18