title: Class with property that can be one of two different types
body: <p>I'm developing a Telegram bot in C# but have difficulty with implementing <code>Message</code> type. According to <a href="https://core.telegram.org/bots/api#message" rel="nofollow">API documentation</a>, <code>chat</code> field can be either of type <code>User</code> or of type <code>GroupChat</code>. How do I implement that in C#?</p>
  
  <p>So far I could only come up with following code using <code>Newtonsoft.Json</code>:</p>
  
  <pre><code>public class Update {
  ....
    [JsonProperty("chat")]
    public User chat { get; set; }
  
    [JsonProperty("chat")]
    public GroupChat group_chat { get; set; }
  ....
  }
  </code></pre>
  
  <p>But it doesn't work with my WebAPI 2 controller method since I deserialize <code>Message</code> using <code>FromBody</code> attribute:</p>
  
  <pre><code>public async Task&lt;HttpResponseMessage&gt; Post(string token, [FromBody] Update update)
  </code></pre>
  
  <p>(type <code>Update</code> has a field <code>message</code> of type <code>Message</code>)</p>
  
  <p>Is there any better way to implement <code>Message</code> type?</p>