title: Get Description Attributes From a Flagged Enum
body: <p>I am trying to create an extension method that will return a <code>List&lt;string&gt;</code> containing all the <code>Description</code> attributes for only the set values of a given <code>[Flags] Enum</code>.</p>
  
  <p>For example, suppose I have the following enum declared in my C# code:</p>
  
  <pre><code>[Flags]
  public enum Result
  {
      [Description("Value 1 with spaces")]
      Value1 = 1,
      [Description("Value 2 with spaces")]
      Value2 = 2,
      [Description("Value 3 with spaces")]
      Value3 = 4,
      [Description("Value 4 with spaces")]
      Value4 = 8
  }
  </code></pre>
  
  <p>And then have a variable set as:</p>
  
  <pre><code>Result y = Result.Value1 | Result.Value2 | Result.Value4;
  </code></pre>
  
  <p>So, the call I want to create would be:</p>
  
  <pre><code>List&lt;string&gt; descriptions = y.GetDescriptions();
  </code></pre>
  
  <p>and the final result would be:</p>
  
  <pre><code>descriptions = { "Value 1 with spaces", "Value 2 with spaces", "Value 4 with spaces" };
  </code></pre>
  
  <p>I have created an extension method for getting <strong>the single description attribute</strong> for an Enum that can not have multiple flags set that is along the following lines:</p>
  
  <pre><code>public static string GetDescription(this Enum value)
  {
      Type type = value.GetType();
      string name = Enum.GetName(type, value);
      if (name != null)
      {
          System.Reflection.FieldInfo field = type.GetField(name);
          if (field != null)
          {
              DescriptionAttribute attr =
                     Attribute.GetCustomAttribute(field,
                       typeof(DescriptionAttribute)) as DescriptionAttribute;
              if (attr != null)
              {
                  return attr.Description;
              }
          }
      }
      return null;
  }
  </code></pre>
  
  <p>And I've found some answers online on how to get ALL the Description attributes for a given Enum type (such as <a href="https://stackoverflow.com/q/1799370/1693085">here</a>), but I'm having problems writing a generic extension method to return the list of descriptions <em>for only the set attributes</em>.</p>
  
  <p>Any help would be really appreciated.</p>
  
  <p>THANKS!!</p>