如何从字符串返回枚举值?
我正在尝试从字符串返回强类型枚举值。我确信有更好的方法来做到这一点。对于像这样的简单事情来说,这似乎是太多的代码:
public static DeviceType DefaultDeviceType
{
get
{
var deviceTypeString = GetSetting("DefaultDeviceType");
if (deviceTypeString.Equals(DeviceType.IPhone.ToString()))
return DeviceType.IPhone;
if (deviceTypeString.Equals(DeviceType.Android.ToString()))
return DeviceType.Android;
if (deviceTypeString.Equals(DeviceType.BlackBerry.ToString()))
return DeviceType.BlackBerry;
if (deviceTypeString.Equals(DeviceType.Other.ToString()))
return DeviceType.Other;
return DeviceType.IPhone; // If no default is provided, use this default.
}
}
想法?
根据从社区获得的反馈,我决定使用将字符串转换为枚举的方法扩展。它采用一个参数(默认枚举值)。该默认值还提供类型,因此可以推断泛型,并且不需要使用 <> 显式指定。该方法现在缩短为:
public static DeviceType DefaultDeviceType
{
get
{
return GetSetting("DefaultDeviceType").ToEnum(DeviceType.IPhone);
}
}
非常酷的解决方案,可以在将来重用。
I'm trying to return a strongly typed Enumeration value from a string. I'm sure there is a better way to do this. This just seems like way too much code for a simple thing like this:
public static DeviceType DefaultDeviceType
{
get
{
var deviceTypeString = GetSetting("DefaultDeviceType");
if (deviceTypeString.Equals(DeviceType.IPhone.ToString()))
return DeviceType.IPhone;
if (deviceTypeString.Equals(DeviceType.Android.ToString()))
return DeviceType.Android;
if (deviceTypeString.Equals(DeviceType.BlackBerry.ToString()))
return DeviceType.BlackBerry;
if (deviceTypeString.Equals(DeviceType.Other.ToString()))
return DeviceType.Other;
return DeviceType.IPhone; // If no default is provided, use this default.
}
}
Ideas?
Based on the feedback I got from the community, I have decided to use a method extension that converts a string to an enumeration. It takes one parameter (the default enumeration value). That default also provides the type, so the generic can be inferred and doesn't need to be specified explicitly using <>. The method is now shortened to this:
public static DeviceType DefaultDeviceType
{
get
{
return GetSetting("DefaultDeviceType").ToEnum(DeviceType.IPhone);
}
}
Very cool solution that can be reused in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
Enum.Parse()
< /a>:如果输入不可靠,您应该小心,因为如果该值无法解释为枚举的值之一,您将收到
ArgumentException
。还有一个
Parse()
的重载,允许您在进行此转换时忽略大小写(如果需要)。Use
Enum.Parse()
:If the input is unreliable, you should take care, because you will get a
ArgumentException
if the value can't be interpreted as one of the values of the enum.There's also an overload of
Parse()
that allows you to ignore case when making this conversion, if needed.如果您正在处理(不可靠的)用户输入,我喜欢使用允许默认值的扩展方法。尝试一下。
If you're dealing with (unreliable) user input, I like to use an extension method that permits a default value. Try it out.
我写了一次这个扩展方法来从字符串转换为任何枚举类型,这样你就不必一遍又一遍地编写转换代码:
要实际使用它,你可以这样做:
myEnum 此时应该等于 ENUM2
I wrote this extension method once to convert to any any enum type from string, this way you don't have to write the conversion code over and over again:
To actually use it you can do this:
myEnum at this point should equal ENUM2