title: How do I convert a dynamically typed object to a type argument <T> in C#?
body: <p>I've googled for hours and also found various stackoverflow questions but none cater to my exact use-case. This is what I have: a method that takes 2 generic type parameters. The library is closed source so I cannot modify it. The method signature looks like:</p>
  
  <pre><code> TTo Convert&lt;TFrom, TTo&gt;(TFrom obj)
  </code></pre>
  
  <p>However when you call this you always have to provide both type parameters, like this:</p>
  
  <pre><code>Convert&lt;Dog, Cat&gt;(dog);
  </code></pre>
  
  <p>I'm trying to support a use-case where only the destination type parameter is provided:</p>
  
  <pre><code>Convert&lt;Cat&gt;(dog);
  </code></pre>
  
  <p>So the method signature I need is as follows:</p>
  
  <pre><code>TTo Convert&lt;TTo&gt;(object obj){
       var TFrom = obj.GetType();
       Convert&lt;TFrom, TTo&gt;((TFrom)obj); // &lt;--- throws an error right here
  }
  </code></pre>
  
  <p>Can you help me fix this? This SO answer mentions using <a href="https://stackoverflow.com/a/43893484/9009598">Lambda expression builders</a> but I'm not sure how to do that in this use case. This SO answer mentions <a href="https://stackoverflow.com/questions/972636/casting-a-variable-using-a-type-variable">dynamic typing</a> but its not what I need here.</p>
  
  <p><strong>Edit:</strong> The <a href="https://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class">linked "duplicate" question</a> comes close but it only handles dynamically casting generic types for regular method arguments, but doesn't handle TYPE arguments which is what I need. </p>