title: Deserialize to IEnumerable class using Newtonsoft Json.Net
body: <p>I have a project that is currently using Json.Net for Json deserialization classes like these:</p>
  
  <pre><code>public class Foo {
      public Guid FooGuid { get; set; }
      public string Name { get; set; }
      public List&lt;Bar&gt; Bars { get; set; }
  }
  
  public class Bar {
      public Guid BarGuid { get; set; }
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>So far it works fine.</p>
  
  <p>To make iteration simpler at one point I made <code>Foo</code> class implement <code>IEnumerable&lt;Bar&gt;</code> like this:</p>
  
  <pre><code>public class Foo : IEnumerable&lt;Bar&gt; {
      public Guid FooGuid { get; set; }
      public string Name { get; set; }
      public List&lt;Bar&gt; Bars { get; set; }
  
      public IEnumerator&lt;Bar&gt; GetEnumerator()
      {
          return Bars.GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  
  public class Bar {
      public Guid BarGuid { get; set; }
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>But then it fails deserializing the object. The error is confusing, as it said that it cannot deserialize <code>FooGuid</code> field, but removing the <code>IEnumerable</code> interface works again.</p>
  
  <p>The problem is the same in both MonoTouch and MonoDroid environments in simulator or device.</p>
  
  <p>Any clue about how to make it work?</p>
  
  <hr>
  
  <p>Added code to reproduce this issue:</p>
  
  <pre><code>public static class Tests {
      public static void SerializeAndDeserializeFoo() {
          // Serialize and deserialize Foo class
          var element = new Foo
          {
              FooGuid = Guid.NewGuid(),
              Name = "Foo name",
              Bars = new List&lt;Bar&gt; {
                  new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
                  new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
                  new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" }
              }
          };
  
          var serializedObject = JsonConvert.SerializeObject(element);
          Console.WriteLine("Serialized Foo element: {0}", serializedObject);
  
          // Exception if Foo implements IEnumerable
          var deserializedObject = JsonConvert.DeserializeObject&lt;Foo&gt;(serializedObject);
          Console.WriteLine("Foo deserialization worked!");
      }
  
      public static void SerializeAndDeserializeEnumerableFoo() {
          // Serialize and deserialize Foo class
          var element = new EnumerableFoo
          {
              FooGuid = Guid.NewGuid(),
              Name = "Foo name",
              Bars = new List&lt;Bar&gt; {
                  new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
                  new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },
                  new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" }
              }
          };
  
          var serializedObject = JsonConvert.SerializeObject(element);
          Console.WriteLine("Serialized EnumerableFoo element: {0}", serializedObject);
  
          try {
              // Exception if Foo implements IEnumerable
              var deserializedObject = JsonConvert.DeserializeObject&lt;EnumerableFoo&gt;(serializedObject);
              Console.WriteLine("EnumerableFoo deserialization worked!");
          }
          catch (Exception e){
              Console.WriteLine("EnumerableFoo deserialization failed!");
              throw;
          }
      }
  }
  
  public class EnumerableFoo : IEnumerable&lt;Bar&gt; {
      public Guid FooGuid { get; set; }
      public string Name { get; set; }
      public List&lt;Bar&gt; Bars { get; set; }
  
      public IEnumerator&lt;Bar&gt; GetEnumerator()
      {
          return Bars.GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  
  public class Foo {
      public Guid FooGuid { get; set; }
      public string Name { get; set; }
      public List&lt;Bar&gt; Bars { get; set; }
  }
  
  public class Bar {
      public Guid BarGuid { get; set; }
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>Sample project: <a href="https://www.dropbox.com/s/27i58aiz71dylkw/IEnumerableJson.zip" rel="noreferrer">https://www.dropbox.com/s/27i58aiz71dylkw/IEnumerableJson.zip</a></p>