title: Generic method to cast one arbitrary type to another in c#
body: <p>I want to do something like this:</p>
  
  <pre><code>public static TResult MyCast&lt;TSource, TResult&gt;(TSource item)
  {
      return (TResult)item;
  }
  </code></pre>
  
  <p>Without restrictions on TSource or TResult and avoiding unnecessary boxing if possible.
  <strong>Edit</strong>: I want to stress out, that I want a simple casting of types, not elaborate type conversion here. It would be perfectly ok to fail at casting, say <code>string</code> to <code>int</code>.</p>
  
  <p>Is there any sane way to do this using CLR 2.0?</p>
  
  <p><strong>Edit</strong>: this is a simplified version, so it's pretty useless, yes.
  But consider casting generic collections, such as this:</p>
  
  <pre><code>public static Dictionary&lt;string, TResult&gt; CastValues&lt;TSource, TResult&gt;(this Dictionary&lt;string, TSource&gt; dictionary)
  </code></pre>
  
  <p>After some discussions with my co-workers, it seems like there's no simple way to implement such a feature (if at all possible), so I'm stuck with code bloat of several very simple methods for different situations (i.e. up- and downcast of reference types and casting of some value types) :(</p>
  
  <p>Too bad I can't use .NET 4.0 with all it's <code>dynamic</code> et al goodness.</p>