title: How do I return a C# web service that has a generic property?
body: <p>I am creating a web service, and want to be a bit more elegant with the return data, instead of having lots of properties that the consumer needs to check.</p>
  
  <p>Depending on what data is generated behind the scenes, I need to be able to return error data, or the data the consumer was expecting.</p>
  
  <p>Instead of having a large flat object, and filling the properties when needed, and letting the user check a 'success' flag, I'd like a single property, Data, to be either an instance of an error class, or an instance of a success class.</p>
  
  <p>This is kind of what I want to do:</p>
  
  <pre><code>class ItemResponse
  {
      public bool Success { get; set; }
      public T Data{ get; set; }
  }
  
  if( /*acceptance criteria*/ )
  {
      ItemResponse&lt;SuccessData&gt; resp = new ItemResponse&lt;SuccessData&gt;();
      resp.Data = new SuccessData();
  }
  else
  {
      ItemResponse&lt;ErrorData&gt; resp = new ItemResponse&lt;ErrorData&gt;();
      resp.Data = new ErrorData();
  }
  
  return resp;
  
  
  public class SuccessData
  {
  
  }
  
  public class ErrorData
  {
  
  }
  </code></pre>
  
  <p>Then have the web method return the object, with the generic property.</p>
  
  <p>Is this possible, and if so, how would I do it, given that the webmethod return type has to be typed correctly?</p>