title: Serializing cyclic object references using DataContractSerializer not working
body: <p>I'm building an XNA game and I'm trying to save game/map etc. state completely, and then be able to load and resume from exactly the same state.</p>
  
  <p>My game logic consists of fairly complex elements (for serializing) such as references, delegates etc. I've done hours of research and decided that it's the best to use a <code>DataContractSerializer</code> that preserves the object references. (I also got around for delegates but that's another topic) I have no problem serializing and deserializing the state, re-creating the objects, the fields, lists, and even object references correctly and completely. But I've got a problem with cyclic references. Consider this scenario:</p>
  
  <pre><code>class X{
     public X another;
  }
  
  //from code:
  X first = new X();
  X second = new X();
  first.another = second;
  second.another = first;
  </code></pre>
  
  <p>Trying to serialize X will result in an exception complaining about cyclic references. If I comment out the last line it works fine. Well, I can imagine WHY it is happening, but I have no idea HOW to solve it. I've read somewhere that I can use the <code>DataContract</code> attribute with <code>IsReference</code> set to true, but it didn't change anything for me -- still got the error. (I want to avoid it anyway since the code I'm working on is portable code and may someday run on Xbox too, and portable library for Xbox doesn't support the assembly that <code>DataContract</code> is in.)</p>
  
  <p>Here is the code to serialize:</p>
  
  <pre><code>class DataContractContentWriterBase&lt;T&gt; where T : GameObject
  {
      internal void Write(Stream output, T objectToWrite, Type[] extraTypes = null)
      {
          if (extraTypes == null) { extraTypes = new Type[0]; }
          DataContractSerializer serializer = new DataContractSerializer(typeof(T), extraTypes, int.MaxValue, false, true, null);
          serializer.WriteObject(output, objectToWrite);
      }
  }
  </code></pre>
  
  <p>and I'm calling this code from this class:</p>
  
  <pre><code>[ContentTypeWriter]
  public class PlatformObjectTemplateWriter : ContentTypeWriter&lt;TWrite&gt;
  (... lots of code ...)
      DataContractContentWriterBase&lt;TWrite&gt; writer = new DataContractContentWriterBase&lt;TWrite&gt;();
      protected override void Write(ContentWriter output, TWrite value)
      {
          writer.Write(output.BaseStream, value, GetExtraTypes());
      }
  </code></pre>
  
  <p>and for deserialization:</p>
  
  <pre><code>class DataContractContentReaderBase&lt;T&gt; where T: GameObject
  {
      internal T Read(Stream input, Type[] extraTypes = null)
      {
          if (extraTypes == null) { extraTypes = new Type[0]; }
          DataContractSerializer serializer = new DataContractSerializer(typeof(T), extraTypes, int.MaxValue, false, true, null);
          T obj = serializer.ReadObject(input) as T;
          //return obj.Clone() as T; //clone falan.. bi bak iste.
          return obj;
      }
  }
  </code></pre>
  
  <p>and it's being called by:</p>
  
  <pre><code>public class PlatformObjectTemplateReader : ContentTypeReader&lt;TRead&gt;
  (lots of code...)
      DataContractContentReaderBase&lt;TRead&gt; reader = new DataContractContentReaderBase&lt;TRead&gt;();
      protected override TRead Read(ContentReader input, TRead existingInstance)
      {
          return reader.Read(input.BaseStream, GetExtraTypes());
      }
  </code></pre>
  
  <p>where:</p>
  
  <p><code>PlatformObjectTemplate</code> was my type to write.</p>
  
  <p>Any suggestions?</p>
  
  <p><strong>SOLUTION</strong>: Just a few minutes ago, I've realized that I wasn't marking the fields with <code>DataMember</code> attribute, and before I added the <code>DataContract</code> attribute, the XNA serializer was somehow acting as the "default" serializer. Now, I've marked all the objects, and things are working perfectly now. I now have cyclic references with no problem in my model.</p>