title: serialize and deserialize self reference object in c#
body: <p>I have a class A like this</p>
  
  <pre><code>class A
      {
          public string Type { get; set; }
          public object[] Content { get; set; }
          public string[] jsonContent { get; set; }
  
          public A()
          {
  
          }
  
          public A(string type)
          {
              this.Type = type;
          }
  
          public A(string type, object[] content)
          {
              this.Type = type;
              this.Content = content;
          }
  
          public string ToJson()
          {
          int len = Content.Length;
          string[] jsonContentTmp = new string[len];
          for (int i = 0; i &lt; Content.Length; ++i)
          {
              jsonContentTmp[i] = JsonConvert.SerializeObject(Content[i]);
          }
          jsonContent = jsonContentTmp;
          var json = JsonConvert.SerializeObject(this);
          return json;
          }
  
          public static A ToA(string str)
          {
          Request a = JsonConvert.DeserializeObject&lt;A&gt;(str);
          return a;
          }
      }
  </code></pre>
  
  <p>Consider bellow: </p>
  
  <pre><code>A sub1 = new A();
  A sub2 = new A();
  object[] obj = {sub1, sub2};
  A test = new A("type", obj);
  </code></pre>
  
  <p>When I want to serialize <code>test</code>, I receive exception </p>
  
  <blockquote>
    <p>self reference</p>
  </blockquote>
  
  <p>I tried <code>PreserveReferencesHandling</code> but i couldn't deserialize and receive exception </p>
  
  <blockquote>
    <p>'cannot preserve reference toarray'</p>
  </blockquote>
  
  <p>.
  Any idea to serialize and deserialize it?</p>