title: DataContract serialization of an inherited type into a base type
body: <p>I'm trying to serialize class <code>B</code> as an instance of ita base class <code>A</code>. The <code>DataContractSerializer</code> won't allow me to do that.</p>
  
  <p>An example failing the serialization is as follows:</p>
  
  <pre><code>class Program
  {
      [DataContract]
      public class A
      {
          public int Id { get; set; }
      }
  
      [DataContract]
      public class B : A
      {
  
      }
  
  
      static void Main(string[] args)
      {
          A instance = new B { Id = 42 };
  
          var dataContractSerializer = new DataContractSerializer(typeof(A));
          var xmlOutput = new StringBuilder();
          using (var writer = XmlWriter.Create(xmlOutput))
          {
              dataContractSerializer.WriteObject(writer, instance);
          }
  
      }
  }
  </code></pre>
  
  <p></p>
  
  <p>I know that the issue is easily resolved by added the <code>KnownTypes</code> attribute.
  However I want to keep the class <code>B</code> hidden from the project (not add a reference). </p>
  
  <p>Is it at all possible to achieve what I want? I tried the <code>XmlSerializer</code> but it gave me the same issue (it added the full instance type name in the XML) and is much more clunkier to use.</p>