title: Intercept async method that returns generic Task<> via DynamicProxy
body: <p>My questions is related to this post <a href="https://stackoverflow.com/questions/14288075/intercept-the-call-to-an-async-method-using-dynamicproxy">Intercept the call to an async method using DynamicProxy</a></p>
  
  <p>I want to implement interceptor that works with async methods that returns <code>Task</code> or <code>Task&lt;T&gt;</code> result. </p>
  
  <p>I use next code for return <code>ContinueWith</code> result (in order that caller method wait while interceptor finishes work)  </p>
  
  <pre><code>var task = invocation.ReturnValue as Task;
  invocation.ReturnValue = task.ContinueWith(c =&gt; 
        { code that should execute after method finish });
  </code></pre>
  
  <p>Above code works fine for <code>Task</code> result, but in case of <code>Task&lt;T&gt;</code> result <code>ContinueWith</code> will change return type from <code>Task&lt;T&gt;</code> to <code>Task</code>.
  I need to call overloaded method ContinueWith that returns <code>Task&lt;T&gt;</code>, but for this I need to cast <code>invocation.ReturnValue</code> to <code>Task&lt;T&gt;</code></p>
  
  <p>I didn't find way to cast it dynamically in any way. 
  Does anyone know how to make it?</p>
  
  <p>I also tried to call this method via reflection, but parameter is labmda function that can't be passed directly.</p>