title: DataContract serialization exception (data contract name is not expected)
body: <p>I have the following code:</p>
  
  <pre><code>[DataContract]
  class TestContract {
      private String _Name;
      private Int32 _Age;
  
      [DataMember( Name = "Name" )]
      public String Name {
          get { return _Name; }
          set { _Name = value; }
      }
  
      [DataMember( Name = "Age" )]
      public Int32 Age {
          get { return _Age; }
          set { _Age = value; }
      }
  }
  
  [Serializable]
  public class DNCJsonDictionary&lt;K, V&gt; : ISerializable {
      Dictionary&lt;K, V&gt; dict = new Dictionary&lt;K, V&gt;();
  
      public DNCJsonDictionary() { }
  
      protected DNCJsonDictionary( SerializationInfo info, StreamingContext context ) {
      }
  
      public void GetObjectData( SerializationInfo info, StreamingContext context ) {
          foreach( K key in dict.Keys ) {
              info.AddValue( key.ToString(), dict[ key ] );
          }
      }
  
      public void Add( K key, V value ) {
          dict.Add( key, value );
      }
  
      public V this[ K index ] {
          set { dict[ index ] = value; }
          get { return dict[ index ]; }
      }
  }
  
  public class MainClass {
      public static String Serialize( Object data ) {
          var serializer = new DataContractJsonSerializer( data.GetType() );
          var ms = new MemoryStream();
          serializer.WriteObject( ms, data );
  
          return Encoding.UTF8.GetString( ms.ToArray() );
      }
  
      public static void Main() {
          DNCJsonDictionary&lt;String, Object&gt; address = new DNCJsonDictionary&lt;String, Object&gt;();
          address[ "Street" ] = "30 Rockefeller Plaza";
          address[ "City" ] = "New York City";
          address[ "State" ] = "NY";
  
          TestContract test = new TestContract();
          test.Name = "CsDJ";
          test.Age = 28;
  
          DNCJsonDictionary&lt;String, Object&gt; result = new DNCJsonDictionary&lt;String, Object&gt;();
          result[ "foo" ] = "bar";
          result[ "Name" ] = "John Doe";
          result[ "Age" ] = 32;
  
          result[ "Address" ] = address;
  
              // ** --- THIS THROWS AN EXCEPTION!!! --- **            
          result[ "test" ] = test;
  
          Console.WriteLine( Serialize( result ) );
  
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>When I run, I get this exception:</p>
  
  <blockquote>
    <p>Type 'Json_Dictionary_Test.TestContract' with data contract name 'TestContract:<a href="http://schemas.datacontract.org/2004/07/Json_Dictionary_Test" rel="noreferrer">http://schemas.datacontract.org/2004/07/Json_Dictionary_Test</a>' is not expected. 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.</p>
  </blockquote>
  
  <p>But I don't understand that! As I know, KnownTypeAttribute is used just in deserialization, and if there is inheritance, isn't it? But here is just serialization. And without the datacontract member works fine.</p>
  
  <p>Any idea?</p>
  
  <hr>
  
  <p>I figured out something that works! There is a parent class with a KnownTypes list, which I fill with all child classes and that will be used in serialization:</p>
  
  <pre><code>[DataContract]
  [KnownType( "GetKnownTypes" )]  // for serialization
  class ResultContract {
      private static List&lt;Type&gt; KnownTypes { get; set; }
  
      public static List&lt;Type&gt; GetKnownTypes() {
          return KnownTypes;
      }
  
      static ResultContract() {
          KnownTypes = new List&lt;Type&gt;();
          try {
              foreach( Type type in Assembly.GetExecutingAssembly().GetTypes() ) {
                  if( !type.IsAbstract &amp;&amp; type.IsSubclassOf( typeof( ResultContract ) ) ) {
                      KnownTypes.Add( type );
                  }
              }
          } catch( Exception ex ) {
              Console.WriteLine( "Fatal error!" );
          }
      }
  }
  
  [DataContract]
  class TestContract : *ResultContract* {
      ...
  }
  ...
  </code></pre>