这是一个帮助方法,可以为您指明正确的方向。
protected Als GetEnumByStringValueAttribute(string value){ Type enumType = typeof(Als); foreach (Enum val in Enum.GetValues(enumType)) { FieldInfo fi = enumType.GetField(val.ToString()); StringValueAttribute[] attributes = (StringValueAttribute[])fi.GetCustomAttributes( typeof(StringValueAttribute), false); StringValueAttribute attr = attributes[0]; if (attr.Value == value) { return (Als)val; } } throw new ArgumentException("The value '" + value + "' is not supported.");}要调用它,只需执行以下操作:
Als result = this.GetEnumByStringValueAttribute<Als>(ComboBox.SelectedValue);
尽管这可能不是最好的解决方案,但它
Als可能与之相关,您可能想使此代码可重复使用。您可能想要从我的解决方案中删除代码以返回属性值,然后
Enum.Parse按您在问题中所使用的方式进行操作。



