title: For WCF, when is it necessary to statically tell a serializer about a data type?
body: <p>I am using the WCF JSON serializer to produce JSON for use as return data for the ASP.NET MVC framework. I am doing this because the built-in JsonAction does not provide any way to control the naming of public properties in the serialized JSON.</p>
  
  <pre><code>public override void ExecuteResult(ControllerContext context)
  {
      ...
  
      if (Data != null)
      {
          var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(Data.GetType());
          System.IO.MemoryStream ms = new System.IO.MemoryStream();
  
          serializer.WriteObject(ms, this.Data);
  
          response.Write(Encoding.Default.GetString(ms.ToArray()));
      }
  }
  </code></pre>
  
  <p>In this example, I am using this with OpenFlashChart, so I set this.Data to a PieChart instance. It worked fine. Then, I set this.Data to an instance of Chart, and I got the following exception:</p>
  
  <p><code>Type 'OpenFlashChart.Pie' with data contract name 'Pie:http://schemas.datacontract.org/2004/07/OpenFlashChart' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</code></p>
  
  <p>Obviously, when I gave the serializer a <code>PieChart</code> element, it was able to infer that I needed the Pie class as well. Why when I provide it <code>Chart&lt;PieChart&gt;</code> is it no longer looking at the classes used by <code>PieChart</code>? Is there a way to get around this issue without having to annotate everything with <code>KnownTypeAttributes</code>?</p>