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">   <span style="color: blue">internal</span> <span style="color: blue">enum</span> <span style="color: #2b91af">SuperHero</span> </pre>
<pre style="margin: 0px">   {</pre>
<pre style="margin: 0px">      [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">"Clark Kent"</span>)]</pre>
<pre style="margin: 0px">      Superman = 1,</pre>
<pre style="margin: 0px">      [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">"Peter Parker"</span>)]</pre>
<pre style="margin: 0px">      SpiderMan = 2,</pre>
<pre style="margin: 0px">      [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">"Bruce Banner"</span>)]</pre>
<pre style="margin: 0px">      Hulk = 4,</pre>
<pre style="margin: 0px">      [<span style="color: #2b91af">Description</span>(<span style="color: #a31515">"Tony Stark"</span>)]</pre>
<pre style="margin: 0px">      IronMan = 8,</pre>
<pre style="margin: 0px">   }</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">      <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">      {</pre>
<pre style="margin: 0px">         <span style="color: green">// Check for Enum that is marked with FlagAttribute</span></pre>
<pre style="margin: 0px">         <span style="color: blue">var</span> entries = value.ToString().Split(ENUM_SEPERATOR_CHARACTER);</pre>
<pre style="margin: 0px">         <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">         <span style="color: blue">for</span> (<span style="color: blue">var</span> i = 0; i < entries.Length; i++)</pre>
<pre style="margin: 0px">         {</pre>
<pre style="margin: 0px">            <span style="color: blue">var</span> fieldInfo = value.GetType().GetField(entries[i].Trim());</pre>
<pre style="margin: 0px">            <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">            description[i] = (attributes.Length > 0) ? attributes[0].Description : entries[i].Trim();</pre>
<pre style="margin: 0px">         }</pre>
<pre style="margin: 0px">         <span style="color: blue">return</span> <span style="color: #2b91af">String</span>.Join(<span style="color: #a31515">", "</span>, description);</pre>
<pre style="margin: 0px">      }</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">      {</pre>
<pre style="margin: 0px">         <span style="color: blue">return</span> GetDescription(value);</pre>
<pre style="margin: 0px">      }</pre>
And now you can do the following:
var secretIdentity = SuperHero.Superman.Description();
<pre style="margin: 0px">         <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">         <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.
Recent Posts
- Understanding API First Strategy and Benefits
- Frankenstein APIs Explained! - API Cyber Security Series
- API Security 101 - Cyber Security Explained
- API Trends 2022 - API Security and Cybersecurity
- API Trends 2022 - Seamless Integration Solutions
- API Trends 2022 - Adaptive API Management
- API Trends 2022 - API Integration Automation
- API Trends 2022 - Industry Specific Breakouts
- API Trends 2022 - API Best Practices
- API Trends 2022 - Open API Standards
- API Trends 2022 - API Integration Experience
- API Trends 2022 - API-Led Modernization
- API Trends 2022 - API Economy Growth
- Brenton House - Give your App and APIs a Turbo Boost – Part 2
- Easily Enable Speech Recognition in Titanium iOS using Hyperloop
Tag Cloud
- .NET
- adaptive api management
- android
- api
- api automation
- api best practices
- api economy
- api experience
- api first
- api integration
- api integrations
- api management
- api security
- api standards
- api strategy
- api trends
- apis
- appcelerator
- automation
- boot
- Brenton House
- C#
- Cisco VPN
- Cloud
- Community News
- Continuous Integration
- crash
- cybersecurity
- Dropbox
- fhir apis
- Free Stuff
- General Software Development
- Google Drive
- graphql
- healthcare apis
- houserules
- hyperloop
- ios
- json schema
- Live Mesh
- Mac OS X
- Microsoft
- mobile
- mobile api
- native
- open banking
- openapi
- Other Stuff
- SkyDrive
- speech
- Stuff
- swagger
- Team Systems
- titanium native
- titanium turbo
- Unit Testing
- Vista
- Visual Studio
- Visual Studio 11
- voice
- win7
- Windows 7
- Windows 8
- windows update