title: Variable generic return type in C#
body: <p>Is there any way to have a method return any one of a number of generic types from a method? For example, I have the following:</p>
  
  <pre><code>public static T ParseAttributeValue&lt;T&gt;(this XElement element, string attribute)
      {
          if(typeof(T) == typeof(Int32))
          {
              return Int32.Parse(element.Attribute(attribute).Value);
          }
  
          if(typeof(T) == typeof(Double))
          {
              return Double.Parse(element.Attribute(attribute).Value);
          }
  
          if(typeof(T) == typeof(String))
          {
              return element.Attribute(attribute).Value;
          }
  
          if(typeof(T) == typeof(ItemLookupType))
          {
              return Enum.Parse(typeof(T), element.Attribute(attribute).Value);
          }
      }
  </code></pre>
  
  <p>(This is only a very quick mockup, I'm aware that any production code would need to be significantly more thorough in null checks etc...)</p>
  
  <p>But the compiler doesn't like it, complaining that <code>Int32</code> cannot be implicitly converted to <code>T</code> (it doesn't work with a cast either). I can understand that. At compile time it has no way to know what <code>T</code> is, but I'm checking it beforehand. Is there anyway I can make this work?</p>