Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get enum from enum attribute

I've got

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

with

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

And I would like to put the value from the item I selected of a combobox into a int:

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

Is this possible, and if so, how? (the StringValue matches the value selected from the combobox).

like image 677
RubenHerman Avatar asked May 07 '10 09:05

RubenHerman


People also ask

How do I find enum attributes?

Enum. GetValues(typeof(FunkyAttributesEnum)); foreach (int value in values) Tuple. Value = Enum. GetName(typeof(FunkyAttributesEnum), value);

Can enum have attributes?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).

What does enum valueOf return?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

What is enum variable?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


2 Answers

Here's a helper method that should point you in the right direction.

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."); } 

And to call it, just do the following:

Als result = this.GetEnumByStringValueAttribute<Als>(ComboBox.SelectedValue); 

This probably isn't the best solution though as it's tied to Als and you'll probably want to make this code re-usable. What you'll probably want to strip out the code from my solution to return you the attribute value and then just use Enum.Parse as you are doing in your question.

like image 164
djdd87 Avatar answered Sep 29 '22 08:09

djdd87


I'm using the DescriptionAttribute from Microsoft and the following extension method:

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; } 
like image 33
Oliver Avatar answered Sep 29 '22 07:09

Oliver