将字符串匹配为枚举? [英] Match string to enumeration?

查看:67
本文介绍了将字符串匹配为枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个第三方DLL,它期望方法签名中的枚举名为"DaysOfWeek".

I am using a 3rd party DLL which expects an enumeration called 'DaysOfWeek' in the method signature.

我允许我的用户从ComboBox中选择星期几,但最后得到一个字符串.

I am allowing my users to select the day of week from a ComboBox, but I end up with a string.

如何将我的字符串与适当的枚举属性匹配?

How can I match my string to the appropriate enumeration property?

谢谢!

推荐答案

Enum.Parse(typeof(DaysOfWeek), yourStringValue, true);

注意:如果字符串不可转换为枚举,则将引发异常.最后一个参数用于不区分大小写的搜索.

Note: This will launch an exception if the string is not convertible to the enum. Last parameter is for case insensitive search.

这篇关于将字符串匹配为枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆