Extension Methods with Enum Description

Since Enum names in .NET do not support certain characters like spaces, often developers will use the DescriptionAttribute to add detailed text to an Enum. 

   [Flags]
<pre style="margin: 0px">&#160;&#160; <span style="color: blue">internal</span> <span style="color: blue">enum</span> <span style="color: #2b91af">SuperHero</span> </pre>

<pre style="margin: 0px">&#160;&#160; {</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">&quot;Clark Kent&quot;</span>)]</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; Superman = 1,</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">&quot;Peter Parker&quot;</span>)]</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; SpiderMan = 2,</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">&quot;Bruce Banner&quot;</span>)]</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; Hulk = 4,</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">&quot;Tony Stark&quot;</span>)]</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; IronMan = 8,</pre>

<pre style="margin: 0px">&#160;&#160; }</pre>

You can pull the description from the enum with code like this.  (Keep in mind that you can make this MUCH faster using DynamicMethods and caching, but that is another article…)

      private const char ENUM_SEPERATOR_CHARACTER = ',';
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">string</span> GetDescription(<span style="color: #2b91af">Enum</span> value)</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; {</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// Check for Enum that is marked with FlagAttribute</span></pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> entries = value.ToString().Split(ENUM_SEPERATOR_CHARACTER);</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> description = <span style="color: blue">new</span> <span style="color: blue">string</span>[entries.Length];</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">for</span> (<span style="color: blue">var</span> i = 0; i &lt; entries.Length; i++)</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> fieldInfo = value.GetType().GetField(entries[i].Trim());</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> attributes = (<span style="color: #2b91af">DescriptionAttribute</span>[])fieldInfo.GetCustomAttributes(<span style="color: blue">typeof</span>(<span style="color: #2b91af">DescriptionAttribute</span>), <span style="color: blue">false</span>);</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; description[i] = (attributes.Length &gt; 0) ? attributes[0].Description : entries[i].Trim();</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #2b91af">String</span>.Join(<span style="color: #a31515">&quot;, &quot;</span>, description);</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; }</pre>

Well, now with .NET 3.5 and Extension methods it only makes sense to make this a little bit easier.

      public static string Description(this Enum value)
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; {</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> GetDescription(value);</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160; }</pre>

And now you can do the following:

         var secretIdentity = SuperHero.Superman.Description();
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> superHeroes = <span style="color: #2b91af">SuperHero</span>.Superman | <span style="color: #2b91af">SuperHero</span>.SpiderMan;</pre>

<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> secretIdentities = superHeroes.Description();</pre>

Note that the code handles Enum marked with the FlagAttribute as well.  The output of the whole flag thing is not ideal but at least it does not break when handling those Enums.

Brenton House

Recent Posts

Tag Cloud

2 Comments

  • Just one tip so eveyone is aware: when youre using both xml comments and DescriptionAttribute, the former will have presedence over the latter.

    In a quick example, using this enum:
    public enum API
    {
    ///
    /// Using Google API
    ///
    [Description("Google API")]
    Google = 1,
    ///
    /// Using Yahoo API
    ///
    [Description("Yahoo API")]
    Yahoo = 2
    }

    and calling API.Yahoo.Description() will yield "Using Yahoo API".

  • How would you write a DynamicMethod for this? &nbsp;I'm new to them, and not sure how it would work.

Comments have been disabled for this content.