我有这样的旗帜:
[Flags]
public enum MyEnum
{
None = 0x00,
Choice1 = 0x01,
Choice2 = 0x02,
Choice3 = 0x04,
Default = Choice1 | Choice2,
All = Default | Choice3
}
我愿以某种方式打印以下国旗:MyEnum.Default
。 在这种情况下,我希望产出像“Choice1, Options2”一样。
仅印刷MyEnum.Default.ToString(<>>>/code>的问题是,如果我想要“Choice1, Options2”,产出将是“Default”。
这里有一个选择,但如果我使用这个办法,每当我改变时,就必须更新印刷。
((StudyData.Choice1 & StudyData.Default) == StudyData.Choice1 ? StudyData.Choice1.ToString() : "") + ", " +
((StudyData.Choice2 & StudyData.Default) == StudyData.Choice2 ? StudyData.Choice2.ToString() : "") + ", " +
((StudyData.Choice3 & StudyData.Default) == StudyData.Choice3 ? StudyData.Choice3.ToString() : "")
Does anyone have a cleaner way of doing this? Ideally, I d like a way of printing out the flags included in MyEnum.Default
without having to change the printing code every time I added a new flag or changed the default.
感谢!