title: Parsing enum flags from comma-separated list or integer
body: <p>I have an XML that contains several flags, some of them are unsigned 32-bit integers and others are unsigned 64-bit integers. Some of them are written in a comma-separated list and others are in hex style.</p>
  
  <p>See this example:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Color&gt;Blue,Red&lt;/Color&gt;
  &lt;Color&gt;0xC&lt;/Color&gt;
  </code></pre>
  
  <p>As I don't want to write a method to parse each enum, I decided to use a generic method. But Visual Studio won't let me build the solution. Here's my method:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static T ParseFlags&lt;T&gt;(string value) where T : struct
  {
      T result = (T)((object)0);
      string[] array;
      // Remove white spaces and delimit string if it is comma-separated
      if (ParseDelimitedString(value, ',', out array))
      {
          for (int i = 0; i &lt; array.Length; i++)
          {
              T flag = (T)((object)0);
              // Check if value is member of enumeration
              if (Enum.TryParse&lt;T&gt;(array[i], out flag))
              {
                  result |= (T)((object)flag);
              }
          }
      }
      else
      {
          switch (Type.GetTypeCode(Enum.GetUnderlyingType(typeof(T))))
          {
              // Remove hex characters and parse node's inner text
              case TypeCode.UInt32:
                  result = (T)((object)ParseUint(value));
                  break;
              case TypeCode.UInt64:
                  result = (T)((object)ParseUlong(value));
                  break;
          }
      }
      return result;
  }
  </code></pre>
  
  <p>The error message I get is:</p>
  
  <blockquote>
    <p>Error 1   Operator '|=' cannot be applied to operands of type 'T' and
    'T'</p>
  </blockquote>
  
  <p>Is there a way to do this?</p>