将标志枚举打印为单独的标志
我有一个如下定义的标志枚举:
[Flags]
public enum MyEnum
{
None = 0x00,
Choice1 = 0x01,
Choice2 = 0x02,
Choice3 = 0x04,
Default = Choice1 | Choice2,
All = Default | Choice3
}
我想要一种方法来打印 MyEnum.Default
中包含哪些标志。在这种情况下,我希望输出类似于“Choice1,Choice2”。
简单打印 MyEnum.Default.ToString()
的问题是,当我想要“Choice1,Choice2”时,输出将是“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() : "")
有没有人有更干净的方法来做到这一点?理想情况下,我想要一种打印 MyEnum.Default 中包含的标志的方法,而不必每次添加新标志或更改默认值时都更改打印代码。
谢谢!
I have a flags enum defined like this:
[Flags]
public enum MyEnum
{
None = 0x00,
Choice1 = 0x01,
Choice2 = 0x02,
Choice3 = 0x04,
Default = Choice1 | Choice2,
All = Default | Choice3
}
I would like a way to print out which flags are included in MyEnum.Default
. In this case, I'd want the output to be something like "Choice1, Choice2".
The problem with simply printing MyEnum.Default.ToString()
is that the output would be "Default" when I want "Choice1, Choice2".
Here's one option, but if I used this I'd have to update the printing every time I changed the enum.
((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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用我在此处相关的扩展方法问题,这应该很简单:
这是扩展方法:
Using the extension methods I've written here on a related question, this should be simple:
And here's the extension methods:
使用 FlagsAttribute 装饰您的枚举。它所做的几乎正是您所追求的:
应该给您:
bar,borkbork
Decorate your enum with FlagsAttribute. It does pretty much exactly what you're after:
should give you:
bar, borkbork
使用
flags.ToString("g");
请参阅枚举格式字符串
Use
flags.ToString("g");
See Enumeration Format Strings
通过单个 linq 语句打印:
更新版本以仅打印显式定义的值:
Print by single linq statement:
Updated version to print only explicitly defined values:
我用最短、最清晰的代码解决了这个问题,我希望它能表现良好,尽管在几个地方有拳击。以您的类型为例:
这就是它的实现方式:
可能会出现一些评论,我将首先解决这些问题:
因为这不能通过隐式的通用
T
参数来完成。T
无法转换为Enum
以便与HasFlag
一起使用。不,也不使用where T : struct, IConvertible
。是的,也能够施展。只有
object
可以转换为其他类型T
、int
、Enum
。我想是的,是的。为了清楚起见,代码是这样写的。所以,是的,如果您愿意,可以这样做并摆脱那些
HasFlag
调用。不可以,因为您需要强制转换为
T
,而这只能从object
完成。可能有“更好”的解决方案,但这肯定是最短、最清晰的解决方案。I solved this in the shortest, clearest code possible that I expect performs well, although there is boxing in a couple of places. Using your type as an example:
This is how it's implemented:
Some comments might come up and I'll address these first:
Because this can't be done with a generic
T
argument implicitly.T
can't be cast toEnum
for use withHasFlag
. No, also not usingwhere T : struct, IConvertible
.Yes, also to be able to cast. Only
object
can be cast to the other typesT
,int
,Enum
.I think so, yes. This code was written like this for clarity. So yes do that and get rid of those
HasFlag
calls if you like.No, because you need a cast to
T
and that can only be done fromobject
. There might be 'better' solutions but this is most certainly the shortest and clearest one.