title: Extending Enums, Overkill?
body: <p>I have an object that needs to be serialized to an EDI format. For this example we'll say it's a car.  A car might not be the best example b/c options change over time, but for the real object the Enums will never change.  </p>
  
  <p>I have many Enums like the following with custom attributes applied.</p>
  
  <pre><code>public enum RoofStyle
  {
      [DisplayText("Glass Top")]
      [StringValue("GTR")]
      Glass,
      [DisplayText("Convertible Soft Top")]
      [StringValue("CST")]
      ConvertibleSoft,
      [DisplayText("Hard Top")]
      [StringValue("HT ")]
      HardTop,
      [DisplayText("Targa Top")]
      [StringValue("TT ")]
      Targa,
  }
  </code></pre>
  
  <p>The Attributes are accessed via Extension methods:</p>
  
  <pre><code>public static string GetStringValue(this Enum value)
  {
      // Get the type
      Type type = value.GetType();
  
      // Get fieldinfo for this type
      FieldInfo fieldInfo = type.GetField(value.ToString());
  
      // Get the stringvalue attributes
      StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
          typeof(StringValueAttribute), false) as StringValueAttribute[];
  
      // Return the first if there was a match.
      return attribs.Length &gt; 0 ? attribs[0].StringValue : null;
  }
  
  public static string GetDisplayText(this Enum value)
  {
      // Get the type
      Type type = value.GetType();
  
      // Get fieldinfo for this type
      FieldInfo fieldInfo = type.GetField(value.ToString());
  
      // Get the DisplayText attributes
      DisplayTextAttribute[] attribs = fieldInfo.GetCustomAttributes(
          typeof(DisplayTextAttribute), false) as DisplayTextAttribute[];
  
      // Return the first if there was a match.
      return attribs.Length &gt; 0 ? attribs[0].DisplayText : value.ToString();
  }
  </code></pre>
  
  <p>There is a custom EDI serializer that serializes based on the StringValue attributes like so:</p>
  
  <pre><code>    StringBuilder sb = new StringBuilder();
      sb.Append(car.RoofStyle.GetStringValue());
      sb.Append(car.TireSize.GetStringValue());
      sb.Append(car.Model.GetStringValue());
      ...
  </code></pre>
  
  <p>There is another method that can get Enum Value from StringValue for Deserialization:</p>
  
  <pre><code>   car.RoofStyle = Enums.GetCode&lt;RoofStyle&gt;(EDIString.Substring(4, 3))
  </code></pre>
  
  <p>Defined as:</p>
  
  <pre><code>public static class Enums
      {
          public static T GetCode&lt;T&gt;(string value)
          {
              foreach (object o in System.Enum.GetValues(typeof(T)))
              {
                  if (((Enum)o).GetStringValue() == value.ToUpper())
                      return (T)o;
              }
              throw new ArgumentException("No code exists for type " + typeof(T).ToString() + " corresponding to value of " + value);
          }
  } 
  </code></pre>
  
  <p>And Finally, for the UI, the <code>GetDisplayText()</code> is used to show the user friendly text.</p>
  
  <p>What do you think?  Overkill?  Is there a better way? or Goldie Locks (just right)?</p>
  
  <p>Just want to get feedback before I intergrate it into my personal framework permanently. Thanks. </p>