title: How to cast object to method return type
body: <p>I want to set <code>args.ReturnValue</code> to instance of an object created from <code>TResponse&lt;T&gt;</code> method called <code>Create</code>.</p>
  
  <pre><code>[Serializable]
  public sealed class LogError : OnMethodBoundaryAspect
  {
      public override void OnException(MethodExecutionArgs args)
      {
          // Logging part..
  
          MethodInfo methodInfo = (MethodInfo)args.Method;
  
          // I want to replace line below comment to get TResponse&lt;T&gt; object instead of dynamic if possible
          dynamic returnValue = Activator.CreateInstance(methodInfo.ReturnType);
          args.ReturnValue = returnValue.Create(CodeMessage.InternalError, MessageType.Error, args.Exception);
  
          args.FlowBehavior = FlowBehavior.Return;
      }
  }
  </code></pre>
  
  <p>Method <code>ReturnType</code> will always be <code>TResponse&lt;T&gt;</code>, but I don't know how to create instance of <code>TResponse&lt;T&gt;</code> based on method return type. <code>TResponse&lt;T&gt;</code> implements method with this signature:</p>
  
  <pre><code>.Create(CodeMessage.InternalError, MessageType.Error, args.Exception);
  </code></pre>
  
  <p><code>Create</code> method is static method that returns <code>TResponse&lt;T&gt;</code> object with parameters set.</p>
  
  <p>Since I didn't know how to do what I want I used <code>Activator</code> to create instance of method return type and store it to <code>dynamic</code> type, but it throws <code>RuntimeBinderException</code> when I call <code>Create</code> method.</p>