C# 将字符串与枚举匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1947817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Match string to enumeration?
提问by user53885
I am using a 3rd party DLL which expects an enumeration called 'DaysOfWeek' in the method signature.
我正在使用第 3 方 DLL,该 DLL 在方法签名中需要一个名为“DaysOfWeek”的枚举。
I am allowing my users to select the day of week from a ComboBox, but I end up with a string.
我允许我的用户从 ComboBox 中选择星期几,但我最终得到了一个字符串。
How can I match my string to the appropriate enumeration property?
如何将我的字符串与适当的枚举属性匹配?
Thanks!
谢谢!
采纳答案by rui
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.
注意:如果字符串不可转换为枚举,这将引发异常。最后一个参数用于不区分大小写的搜索。
回答by Reed Copsey
You can use Enum.Parseto convert the string to the actual Enum.
您可以使用Enum.Parse将字符串转换为实际的 Enum。
回答by LBushkin
You can use the following to convert a string to an enum:
您可以使用以下内容将字符串转换为枚举:
DaysOfWeek value = (DaysOfWeek)Enum.Parse( typeof(DaysOfWeek), enumAsString );
You can also use the case-insensitive overload if users may type in theor own values:
如果用户可以输入理论或自己的值,您还可以使用不区分大小写的重载:
DaysOfWeek value = (DaysOfWeek)Enum.Parse( typeof(DaysOfWeek), enumAsString, true );
回答by Jeffrey L Whitledge
You can use the Parse method on the Enum static type.
您可以对 Enum 静态类型使用 Parse 方法。
DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), (string)dropDownList.SelectedItem);
Alternatively, you can populate the drop-down-list with enum values directly and avoid the conversion issues entirely.
或者,您可以直接使用枚举值填充下拉列表并完全避免转换问题。
// Populate combobox.
foreach (DaysOfWeek day in Enum.GetValues(typeof(DaysOfWeek)))
{
daysOfWeekCombo.Items.Add(day);
}
// Read combobox.
DaysOfWeek day = (DaysOfWeek)daysOfWeekCombo.SelectedItem;
回答by Jo?o Angelo
Assuming the combo box contains the enumeration names.
假设组合框包含枚举名称。
ComboBox cb = new ComboBox();
cb.Items.AddRange(Enum.GetNames(typeof(DaysOfWeek));
(DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), cb.SelectedText);
回答by BFree
Create a class that looks something like this:
创建一个看起来像这样的类:
public class DayOfWeekComboBoxItem
{
public string Day{get;set;}
public DayOfWeek DayOfWeek{get;set;}
public override ToString()
{
return this.Date;
}
}
Then bind your combo box to a collection of these items. Then you can cast the SelectedItem in the combo box to an instance of that class.
然后将您的组合框绑定到这些项目的集合。然后,您可以将组合框中的 SelectedItem 转换为该类的实例。
回答by Oliver Friedrich
Sorry for cross-referencing, but in my anser here theres a helper class called EnumPair<T>
That you could use to fill the ComboBox and have the value of the enum back to use further on.
很抱歉交叉引用,但在我的 anser 中,有一个名为EnumPair<T>
That you can use to fill ComboBox的辅助类,并将枚举的值返回以供进一步使用。
using this three lines will fill the comboBox correctly for you:
使用这三行将为您正确填充组合框:
cbo.DataSource = EnumPair<TEnum>.GetValuePairList();
cbo.ValueMember = EnumPair<TEnum>.ValueMember;
cbo.DisplayMember = EnumPair<TEnum>.DisplayMember;
Here is the original posting
这是原帖
回答by Nathen Silver
If you are populating your ComboBox from the DaysOfWeek enum:
如果您要从 DaysOfWeek 枚举填充 ComboBox:
this.comboBox1.DataSource = Enum.GetValues(typeof(DayOfWeek));
Then you can get the enum value from the SelectedItem property of the ComboBox:
然后你可以从 ComboBox 的 SelectedItem 属性中获取枚举值:
DayOfWeek selected = (DayOfWeek)this.comboBox1.SelectedItem;
回答by Michael Stum
Apart from the other (correct and built-in) suggestions, there is also an interesting Third-Party Enum<T>
class that makes working with enums much more nice:
除了其他(正确的和内置的)建议之外,还有一个有趣的第三方Enum<T>
类,它使使用枚举变得更好:
http://blog.logrythmik.com/post/Better-Enum-Parsing-using-Strongly-Typed-Generics.aspx
http://blog.logrythmik.com/post/Better-Enum-Parsing-using-Strongly-Typed-Generics.aspx
回答by Joe
You can bind to a list of name/value pairs, where the value is your enum. A bit more work, but will make it easier to support localization down the line if you ever need it. Here's one way:
您可以绑定到名称/值对列表,其中值是您的枚举。需要做更多的工作,但如果您需要,可以更轻松地支持本地化。这是一种方法:
static IList<KeyValuePair<string, DayOfWeek>> GetDayOfWeekBindingList()
{
List<KeyValuePair<string, DayOfWeek>> dayOfWeekList = new List<KeyValuePair<string, DayOfWeek>>();
foreach (int day in Enum.GetValues(typeof(DayOfWeek)))
{
string dayName = CultureInfo.CurrentCulture.DateTimeFormat.DayNames[day];
dayOfWeekList.Add(new KeyValuePair<string, DayOfWeek>(dayName, (DayOfWeek)day));
}
return dayOfWeekList;
}
private void BindDayOfWeekComboBox()
{
dayOfWeekComboBox.DisplayMember = "Key";
dayOfWeekComboBox.ValueMember = "Value";
dayOfWeekComboBox.DataSource = GetDayOfWeekBindingList();
}
private void Form1_Load(object sender, EventArgs e)
{
BindDayOfWeekComboBox();
...
}
You can then access the day of week as:
然后,您可以通过以下方式访问星期几:
(DayOfWeek) dayOfWeekComboBox.SelectedValue;