如何将字符串转换为给定的枚举
如何转换字符串枚举?
我有下面的代码,当我尝试将字符串分配给 levelEnum 时,它给了我错误,其中 levelEnum 是一个枚举。
foreach (CustomProperty prop in requirementTemplate.AttributesCustomList)
{
if (prop.Name == property)
{
return (CRF_DB.CRF_Requirement.LevelEnum) (prop.Value.ToString());
}
}
有没有办法通过为其赋值来放置选择枚举项?
希望它足够清楚
how to cast string enum ?
i have the code below , it gives me error when i try to assign string to levelEnum, where levelEnum is an Enumeration..
foreach (CustomProperty prop in requirementTemplate.AttributesCustomList)
{
if (prop.Name == property)
{
return (CRF_DB.CRF_Requirement.LevelEnum) (prop.Value.ToString());
}
}
Is there a way to put select Enum item by assigning value to it ?
hope it is clear enough
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下操作
Try the following
查看 Enum.TryParse
http://msdn.microsoft.com/en-us/库/dd783499.aspx
Look at Enum.TryParse
http://msdn.microsoft.com/en-us/library/dd783499.aspx
为了避免异常,您可以通过调用
IsDefined
检查该值是否存在于该枚举中。如果您使用 .NET 4.0,则TryParse
方法将是最佳解决方案。In order to avoid an exception you can check if the value exists within that enumeration by calling
IsDefined
. TheTryParse
method would be the optimal solution if you're using .NET 4.0.