title: How to make a serialization with recursive traversal?
body: <p>My serializer serializes  properties and fields.<br>
  If there will be a custom complex type, my serializer won't work.<br>
  I would like to add a serialization of custom complex types and allow possibility of doing recursive traversal. 
  I'm new to programming and frankly I never had deal with recursive traversal.</p>
  
  <p>This is what I have now:</p>
  
  <pre><code>internal class MySerializer
  {
      private Type targetType;
  
      public MySerializer(Type targetType)
      {
          this.targetType = targetType;
          if (!targetType.IsDefined(typeof (DataContractAttribute), false))
              throw new Exception("No soup for you");
      }
  
      public void WriteObject(Stream stream, Object graph)
      {
          IEnumerable&lt;MemberInfo&gt; serializbleMembers =
              targetType.GetMembers(BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance |
                                    BindingFlags.Public | BindingFlags.NonPublic)
                  .Where(p =&gt; p.IsDefined(typeof (DataMemberAttribute), false));
          var writer = new StreamWriter(stream);
          writer.WriteLine("&lt;" + targetType.Name + "&gt;");
          foreach (MemberInfo memberInfo in serializbleMembers)
          {
              writer.Write("\t&lt;" + memberInfo.Name + "&gt;");
              var fieldInfo = memberInfo as FieldInfo;
              if (fieldInfo != null)
              {
                  writer.Write(fieldInfo.GetValue(graph).ToString());
              }
  
              var propInfo = memberInfo as PropertyInfo;
              if (propInfo != null)
              {
                  writer.Write(propInfo.GetValue(graph, null).ToString());
              }
  
              writer.Write("&lt;/" + memberInfo.Name + "&gt;");
          }
          writer.WriteLine("&lt;/" + targetType.Name + "&gt;");
          writer.Flush();
      }
  }
  
  
  [DataContract]
  class Person 
  {
      [DataMember]
      public string _family;
      [DataMember]
      public string FirstName { get; set; }
      [DataMember]
      public string LastName { get; set; }
      [DataMember]
      public int Age { get; set; }      
   }
  </code></pre>