Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use the following class</p> <pre><code> public class EnumUtils { /// &lt;summary&gt; /// Reads and returns the value of the Description Attribute of an enumeration value. /// &lt;/summary&gt; /// &lt;param name="value"&gt;The enumeration value whose Description attribute you wish to have returned.&lt;/param&gt; /// &lt;returns&gt;The string value portion of the Description attribute.&lt;/returns&gt; public static string StringValueOf(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length &gt; 0) { return attributes[0].Description; } else { return value.ToString(); } } /// &lt;summary&gt; /// Returns the Enumeration value that has a given Description attribute. /// &lt;/summary&gt; /// &lt;param name="value"&gt;The Description attribute value.&lt;/param&gt; /// &lt;param name="enumType"&gt;The type of enumeration in which to search.&lt;/param&gt; /// &lt;returns&gt;The enumeration value that matches the Description value provided.&lt;/returns&gt; /// &lt;exception cref="ArgumentException"&gt;Thrown when the specified Description value is not found with in the provided Enumeration Type.&lt;/exception&gt; public static object EnumValueOf(string value, Type enumType) { string[] names = Enum.GetNames(enumType); foreach (string name in names) { if (StringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value)) { return Enum.Parse(enumType, name); } } throw new ArgumentException("The string is not a description or value of the specified enum."); } </code></pre> <p>Which reads an attribute called description</p> <pre><code>public enum PuppyType { [Description("Cute Puppy")] CutePuppy = 0, [Description("Silly Puppy")] SillyPuppy } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload