title: Is it possible to have a method in C# that implicitly deserializes an argument if it's passed as a JSON string?
body: <h1>Question</h1>
  <p>I have a handful of ViewComponents that look like so:</p>
  <pre><code>public IViewComponentResult Invoke(BuyerViewModel buyer)
  </code></pre>
  <p>I'd like them to be able to accept either a <code>BuyerViewModel</code>, or a JSON string representing a <code>BuyerViewModel</code>. For example, when you pass JSON to a controller method from JavaScript, if that method expects an argument of type <code>Dog</code>, the controller automatically attempts to deserialize the JSON to an instance of <code>Dog</code>. I'm trying to mimic that behavior.</p>
  <p>The goal would be that <strong>both</strong> of these examples work:</p>
  <pre><code>var buyer = new BuyerSummaryViewModel() { FirstName = &quot;John&quot; };
  ViewComponent(&quot;Buyer&quot;, buyer);
  </code></pre>
  
  <pre><code>ViewComponent(&quot;Buyer&quot;, &quot;{\&quot;Name\&quot;:\&quot;John Smith\&quot;}&quot;);
  </code></pre>
  <hr />
  <h2>Why?</h2>
  <p>I'm trying to make a generic JavaScript method that can fetch a ViewComponent on the fly:</p>
  <pre class="lang-js prettyprint-override"><code>const fetchViewComponent = async (viewComponentName, viewModel) =&gt; {
    let data = { viewComponentName, viewModel };
    let html = await $.get(`/Order/FetchViewComponent`, data);
    return html;
  }
  
  //Get a BuyerViewComponent (example)
  (async () =&gt; {
    let component = await fetchViewComponent(&quot;Buyer&quot;, `@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Buyer))`);
    console.log(component);
  })();
  </code></pre>
  <hr />
  <h2>What I've Tried</h2>
  <p>If I specify that the ViewModel is a <code>BuyerViewModel</code>, it works. The JSON string is automatically deserialized into a <code>BuyerViewModel</code>.</p>
  <pre><code>public class FetchViewComponentRequest
  {
    public string ViewComponentName { get; set; }
    public BuyerViewModel ViewModel { get; set; }
    //     ^^^^^^^^^^^^^^
  }
  
  [HttpGet]
  public IActionResult FetchViewComponent(FetchViewComponentRequest request)
  {
    return ViewComponent(request.ViewComponentName, request.ViewModel);
  }
  </code></pre>
  <hr />
  <h2>The Issue</h2>
  <p>However, I don't want to specify the type; I want this to be <em>generic</em>. So I tried this:</p>
  <pre><code>public class FetchViewComponentRequest
  {
    public string ViewComponentName { get; set; }
    public string ViewModel { get; set; }
    //     ^^^^^^
  }
  
  [HttpGet]
  public IActionResult FetchViewComponent(FetchViewComponentRequest request)
  {
    return ViewComponent(request.ViewComponentName, request.ViewModel);
  }
  </code></pre>
  <p>But as expected, <code>request.ViewModel</code> isn't the correct type; it ends up <code>null</code> in the <code>Invoke</code> method. I was hoping there was a flag or something more global I could specify so that it tries to implicitly deserialize this string into the expected type.</p>
  <p>Is there an easier way to do this that I haven't considered? Or, if not, is the way I'm envisioning even possible?</p>
  <p>(I'm using <strong>.NET Core 2.2</strong>)</p>