所有枚举项转为字符串 (C#)
如何将所有元素从枚举转换为字符串?
假设我有:
public enum LogicOperands {
None,
Or,
And,
Custom
}
我想要归档的内容类似于:
string LogicOperandsStr = LogicOperands.ToString();
// expected result: "None,Or,And,Custom"
How to convert all elements from enum to string?
Assume I have:
public enum LogicOperands {
None,
Or,
And,
Custom
}
And what I want to archive is something like:
string LogicOperandsStr = LogicOperands.ToString();
// expected result: "None,Or,And,Custom"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你必须做这样的事情:
或者在 Linq 中:
You have to do something like this:
Or in Linq:
虽然@Moose的答案是最好的,但我建议您缓存该值,因为您可能会经常使用它,但它在执行过程中100%不可能改变——除非您修改并重新编译枚举。 :)
就像这样:
Although @Moose's answer is the best, I suggest you cache the value, since you might be using it frequently, but it's 100% unlikely to change during execution -- unless you're modifying and re-compiling the enum. :)
Like so:
将枚举转换为可以交互的内容的简单而通用的方法:
然后:
A simple and generic way to convert a enum to something you can interact:
and then: