title: Return different Object (List or error class) from WCF service
body: <p>I am trying to build a WCF service which has a single method <code>getPersonList</code> which returns list of person like this</p>
  
  <pre><code>[
    {"personId":121, "personName":"John Smith"},
    {"personId":953, "personName":"Maggie Johnson"}
  ]
  </code></pre>
  
  <p>If there is some error, I want to return an error response like this from the same method.</p>
  
  <pre><code>{"errorCode":4002,"errorMessage":"invalid request token"}
  </code></pre>
  
  <p>Now I have my Service Contract as below:</p>
  
  <pre><code>    [ServiceContract()]
      public interface IPersonContract
      {
          [OperationContract()]
          Object GetPersonList(int requestParam);
      }
  </code></pre>
  
  <p>and my sample <code>GetPersonList</code> method</p>
  
  <pre><code>Object GetPersonList(int requestParam)
  {
    if (requestParam == 1)
    {
      ErrorResponse error = new ErrorResponse();
      error.ErrorCode = 4002;
      error.ErrorMessage = "invalid request token";
  
      return error;
    }else
    {
      List&lt;Person&gt; returnList = new List&lt;Person&gt;();
      // add Person to returnList
      return returnList;
    }
  
  }
  </code></pre>
  
  <p>Person Class</p>
  
  <pre><code>[DataContract()]
  public class Person
  {
  
      [DataMember(Name = "personName")]
      String PersonName{   get;   set;  }
  
      [DataMember(Name = "personId")]
      String PersonID {   get;   set;  }
  
  }
  </code></pre>
  
  <p>Error Class</p>
  
  <pre><code>[DataContract()]
  public class ErrorResponse
  {
  
      [DataMember(Name = "errorCode")]
      int ErrorCode{   get;   set;  }
  
      [DataMember(Name = "errorMessage")]
      String ErrorMessage{   get;   set;  }
  
  }
  </code></pre>
  
  <p>I looked up <code>KnownTypes</code> for DataContract classes but how do I apply that on <code>Object</code>. </p>
  
  <p>If I add fields from <code>ErrorReponse</code> and add <code>List&lt;Person&gt;</code> in a single class and return that object, I get a response like this in success case which is not what I want.</p>
  
  <pre><code>{
  "Person":[{"personId":121, "personName":"John Smith"},
        {"personId":953, "personName":"Maggie Johnson"}]
  }
  </code></pre>