title: Cast causes "Operation could destabilize the runtime" on one ASP.NET 4.0 box and not another
body: <p>Here's (part of) a simple deserialization method that I've used in .NET 2.0 for years.  <code>T</code> is an unconstrained type parameter.</p>
  
  <pre><code>protected virtual T ItemFromString(string s) {
  
      if (typeof(T).IsPrimitive ||
          typeof(T) == typeof(string)) {
  
          try {
              // needed for string, too: compiler doesn't allow (T)s
              return (T)Convert.ChangeType(s, typeof(T));
          }
          catch (Exception ex) {
              // stuff
          }
      }
  }
  </code></pre>
  
  <p>I changed the application pool to run in 4.0, and everything was fine &#8212; until I deployed it to the remote server.  There, I get "Operation could destabilize the runtime" on the line</p>
  
  <pre><code>return (T)Convert.ChangeType(s, typeof(T));
  </code></pre>
  
  <p>(Actually the line reported is the ending brace of the method, but I've narrowed it down to that line.)</p>
  
  <p>The problem goes away if I change the runtime back to 2.0.</p>
  
  <p>The runtime versions are both <code>4.0.30319</code>.  The app is otherwise identical, including <code>web.config</code>.  Both apps using shared application pools and running in Full trust.  Presumably another setting is affecting it, but I have no idea how to figure out what.</p>
  
  <p>Everything I've found about this involves <code>Reflection.Emit</code> or covariance, which I'm not using.</p>
  
  <p>Any leads?</p>
  
  <p>Thanks.</p>