有没有一种简单的方法可以将 ac# 枚举转换为字符串,然后再转换回来?
我需要将枚举值列表转换为单个字符串以存储在我的数据库中;然后当我从数据库检索时再次转换回来。
每个枚举的值目前都是一个简单的整数,因此创建一个额外的表来处理这个问题感觉有点过分了。
因此,在下面的示例中,如果用户选择“母亲”、“父亲”和“姐妹”,那么数据库中存储的值将为“0,1,3”
public enum MyEnum
{
Mother = 0,
Father = 1,
Gran = 2,
Sister = 3,
Brother = 4
}
,我是 c# 新手,所以不确定是否有一个不错的输出 -开箱即用的方式来做到这一点 - 我在谷歌搜索时找不到任何明显的东西!
提前干杯:) -L
I need to convert a List of enums values to a single string to store in my db; then convert back again when I retrieve from the database.
Each enum's value is currently a simple integer, so it feels a bit overkill to create an extra table to deal with this.
So, in the example below, if the user selects Mother, Father and Sister, then the value stored in the database will be "0,1,3"
public enum MyEnum
{
Mother = 0,
Father = 1,
Gran = 2,
Sister = 3,
Brother = 4
}
I'm new to c#, so not sure if there is a nice out-the-box way to do this - I couldn't find anything obvious when google hunting!
Cheers in advance :)
- L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您将枚举值更改为:
那么您可以
将Father和Gran存储为6 Sister,将Brother存储为24等,您不应该通过组合它们来获得重复值
使用二进制数
If you change you enum values to:
Then you could store Father and Gran as 6
Sister and Brother as 24 etc
by using binary numbers you should not get duplicate values by combining them
以下将根据要求通过“0,1,3”在枚举值数组之间来回转换:
The following will convert back and forth between an array of Enum values via "0,1,3" as requested:
从你的代码来看是
from your code it is
只需使用
ToString
转换为名称,并使用Enum。 TryParse
(或Enum.Parse
(如果您不在 .NET 4)上转换回来。如果您希望一个枚举字段包含多个值(例如 MyEnum.Mother | MyEnum.Father),则需要将其转换为
Flags
枚举,如 @WraithNath 建议。否则,您将讨论单独存储每个选项(无法将Mother
和Father
存储在当前设置的同一字段中)。Just use
ToString
to convert to the name, and the useEnum.TryParse
(orEnum.Parse
if you're not on .NET 4) to convert back.If you're wanting one enum field to contain multiple values (e.g MyEnum.Mother | MyEnum.Father), you'll need to convert that to a
Flags
enum, as @WraithNath suggested. Otherwise you're talking about storing each option separately (there's no way to storeMother
andFather
in the same field with your current setup).字符串等价
解析
String Equivelant
Parsing
Enum
可以与整数值进行转换。这可能是你最好的选择。如果你真的想使用字符串,
ToString
将返回“Mother”“Father”“Gran”等。从字符串强制转换只是一个函数:编辑:
Ian 对于回溯的回答是更“C#-ey”的方式,并且可扩展性更强(可以更灵活地向枚举添加新值)。
Enum
s can be cast to and from integer values. That's probably your best bet.If you really want to use strings,
ToString
will return "Mother" "Father" "Gran" etc. Casting back from a string would just be a function:EDIT:
Ian's answer for casting back is the more "C#-ey" way of doing it, and is far more extensible (handles adding new values to the enum much more flexibly).
根据 Jamiec 的建议(非常适合您的需求),如果您需要防御性地对您的演员进行编码,请尝试:
Further to Jamiec's suggestion (which fits well for your need), if you need to defensively code your casting, try:
枚举可以明确地转换为整数
或
从整数转换,对于字符串使用
ToString
和Enum.Parse
和
Enum's are explicitely able to be cast to/from integers
and
For strings use
ToString
andEnum.Parse
and