title: C# generic casting exception
body: <p>I have run into an odd casting issue while using generics. The following code will throw an <code>InvalidCastException</code> even though clearly an <code>int</code> can be cast to a <code>double</code>. Can anyone explain this behaviour and how to bypass it?</p>
  
  <pre><code>public class TestClass&lt;T&gt;
  {
      public T Cast(object o)
      {
          return (T)o;
      }
  }
  
  public void Main()
  {
      TestClass&lt;double&gt; w = new TestClass&lt;double&gt;();
      double x = w.Cast(10);
  }
  </code></pre>
  
  <p><b>Edit:</b></p>
  
  <p>Since I am in .net 4.0 land anyway I have changed it to use 'dynamic' instead of 'object' and everything works as expected. Appreciate all the spot-on and quick replies.</p>