title: Implementing IFormatter recursively
body: <p>I'm trying to implement a custom formatter using the .NET IFormatter interface. </p>
  
  <p>After a couple of hours of search, I just found a very basic sample which unfortunately doesn't include recursion. I also tried with Reflector to look at BinaryFormatter and SoapFormatter, but they are rather complex.</p>
  
  <p>My question is:
  Should I implement recursion myself, or there's something I've missed in FormatterServices?</p>
  
  <p>Following my code:</p>
  
  <pre><code>public void Serialize(Stream serializationStream, object graph)
  {
      // Get fields that are to be serialized.
      MemberInfo[] members = FormatterServices.GetSerializableMembers(graph.GetType(), Context);
  
      // Get fields data.
      object[] data = FormatterServices.GetObjectData(graph, members);
  
      // Write class name and all fields &amp; values to file
      StreamWriter sw = new StreamWriter(serializationStream);
  
      string accumulator = string.Empty;
      for (int i = 0; i &lt; data.Length; ++i)
      {
          // Skip this field if it is marked NonSerialized.
          if (Attribute.IsDefined(members[i], typeof(NonSerializedAttribute)))
              continue;
  
          FieldInfo field = (FieldInfo)members[i];
          if (field.FieldType.IsPrimitive)
          {
          }
          else //TODO: What should I do here?
      }
      sw.Close();
  }
  </code></pre>