title: Using generics in interfaces and property
body: <p>I am trying to create a bunch of classes/interfaces for use in defining HTTP requests.</p>
  <p>The <code>Request</code> class is defined as below</p>
  <pre><code>internal class Request : IRequest
  {
      public Request(Uri uri, Method method, Headers headers)
      {
          Uri = uri;
          Method = method;
          Headers = Headers;
      }
  
      public IRequestBody Body { get; set; }
  }
  </code></pre>
  <p>For <code>IRequestBody</code> I want to be able to define a Type, based on what the request is. For example, a request might contain JSON data, or Form Data.</p>
  <p>I tried to define <code>IRequestBody</code> as</p>
  <pre><code>public interface IRequestBody&lt;T&gt;
  {
      RequestBodyType Type { get; }
  
      T Payload { get; }
  }
  </code></pre>
  <p>With the hope that I could then define classes such as:</p>
  <pre><code>public class FormDataRequestBody : IRequestBody&lt;NameValueCollection&gt;
  {
      public RequestBodyType Type =&gt; RequestBodyType.FormData;
  
      public NameValueCollection Payload =&gt; new NameValueCollection();
  }
  </code></pre>
  <p>However, on the <code>Request</code> class I must define the Type in the <code>Body</code> property definition. How can I make <code>Body</code> on the <code>Request</code> class generic so I can pass any instance of <code>IRequestBody</code> without knowing the Type upfront?</p>