title: Generic method to type casting
body: <p>I'm trying to write generic method to cast types. I want write something like <code>Cast.To&lt;Type&gt;(variable)</code> instead of <code>(Type) variable</code>.
  My wrong version of this method:</p>
  
  <pre><code>public class Cast
  {
      public static T To&lt;T&gt;(object o)
      {
          return (T) o;
      }
  }
  </code></pre>
  
  <p>And this is simple test:</p>
  
  <pre><code>public class A
  {
      public static explicit operator B(A a)
      {
          return new B();
      }
  }
  
  public class B
  {
  }
  
  A a = new A();
  B b = Cast.To&lt;B&gt;(a);
  </code></pre>
  
  <p>As you guessed, this code will fail with <code>InvalidCastException</code>.</p>
  
  <p>Is this code fail because virtual machine doesn't know how to cast variable of type <code>object</code> to type <code>B</code> at run-time? But exception message says: "unable to cast object of type A to type B". So CLR knows about real type of variable <code>o</code>, why it cannot perform casting?</p>
  
  <p>And here is main question: how should I rewrite method <code>T To&lt;T&gt;(object o)</code> to fix this problem?</p>