title: Casting value to T in a generic method
body: <p>I have an interface for a creaky property-map:</p>
  
  <pre><code>interface IPropertyMap
  {
     bool Exists(string key);
     int GetInt(string key);
     string GetString(string key);
     //etc..
  }
  </code></pre>
  
  <p>I want to create an extension method like so:</p>
  
  <pre><code>public static T GetOrDefault&lt;T&gt;(this IPropertyMap map, string key, T defaultValue)
  {
      if (!map.Exists(key))
          return defaultValue;
      else
      {
          if (typeof(T) == typeof(int)) return (T)map.GetInt(key);
          //etc..
      }
  }
  </code></pre>
  
  <p>But the compiler won't let me cast to T. I tried adding <code>where T : struct</code> but that doesn't seem to help. </p>
  
  <p>What am I missing?</p>