从枚举属性获取枚举

我有

public enum Als { [StringValue("Beantwoord")] Beantwoord = 0, [StringValue("Niet beantwoord")] NietBeantwoord = 1, [StringValue("Geselecteerd")] Geselecteerd = 2, [StringValue("Niet geselecteerd")] NietGeselecteerd = 3, } 

 public class StringValueAttribute : Attribute { private string _value; public StringValueAttribute(string value) { _value = value; } public string Value { get { return _value; } } } 

我想把我从一个combobox中select的项目的值放到一个int中:

 int i = (int)(Als)Enum.Parse(typeof(Als), (string)cboAls.SelectedValue); //<- WRONG 

这是可能的,如果是这样,怎么样? ( StringValue匹配从combobox中select的值)。

这是一个辅助方法,应该指向正确的方向。

 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); 

这可能不是最好的解决scheme,因为它与Als绑定在一起,你可能想要使这个代码重用。 你可能想从我的解决scheme中Enum.Parse代码,返回属性值,然后像在你的问题中一样使用Enum.Parse

我使用Microsoft的DescriptionAttribute和下面的扩展方法:

 public static string GetDescription(this Enum value) { if (value == null) { throw new ArgumentNullException("value"); } string description = value.ToString(); FieldInfo fieldInfo = value.GetType().GetField(description); DescriptionAttribute[] attributes = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { description = attributes[0].Description; } return description; } 

这里有几个扩展方法,我用这个确切的目的,我已经重写了这些使用您的StringValueAttribute ,但像Oliver我在我的代码中使用DescriptionAttribute 。

  public static T FromEnumStringValue<T>(this string description) where T : struct { Debug.Assert(typeof(T).IsEnum); return (T)typeof(T) .GetFields() .First(f => f.GetCustomAttributes(typeof(StringValueAttribute), false) .Cast<StringValueAttribute>() .Any(a => a.Value.Equals(description, StringComparison.OrdinalIgnoreCase)) ) .GetValue(null); } 

在.NET 4.5中,这可以做得更简单一些:

  public static T FromEnumStringValue<T>(this string description) where T : struct { Debug.Assert(typeof(T).IsEnum); return (T)typeof(T) .GetFields() .First(f => f.GetCustomAttributes<StringValueAttribute>() .Any(a => a.Value.Equals(description, StringComparison.OrdinalIgnoreCase)) ) .GetValue(null); } 

要调用它,只需执行以下操作:

 Als result = ComboBox.SelectedValue.FromEnumStringValue<Als>(); 

相反 ,这是一个从枚举值中获取string的函数:

  public static string StringValue(this Enum enumItem) { return enumItem .GetType() .GetField(enumItem.ToString()) .GetCustomAttributes<StringValueAttribute>() .Select(a => a.Value) .FirstOrDefault() ?? enumItem.ToString(); } 

并称之为:

 string description = Als.NietBeantwoord.StringValue() 

不知道如果我在这里失去了一些东西,你不能这样做,

 Als temp = (Als)combo1.SelectedItem; int t = (int)temp;