StackOverflowException when serializing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • etienne.maitre@fr.michelin.com

    StackOverflowException when serializing

    Hi,

    I work against .NET 2.0 using C# 2.0. I am facing a problem trying to
    serialize the class foo (given below). Basically, foo can contain
    other foos in a list. When I try to create a serializer, using the
    test() method below, I get a superb StackOverflowEx ception. I googled
    for it but didn't found any answer to why I got this exception and how
    to serialize foo.

    Any help accepted :-)

    Thanks,

    public void test()
    {
    XmlSerializer serializer = new XmlSerializer(t ypeof(foo));
    ....
    }

    [Serializable]
    public class foo : IEnumerable<foo >
    {
    // ... some members...
    List<foog;

    public foo()
    {
    g = new List<foo>();
    }

    public IEnumerator<foo GetEnumerator()
    {
    foreach (foo element in g)
    {
    yield return element;
    }
    }

    System.Collecti ons.IEnumerator
    System.Collecti ons.IEnumerable .GetEnumerator( )
    {
    return GetEnumerator() ;
    }

    public void Add(Object e)
    {
    g.Add(e as foo);
    }
    }

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: StackOverflowEx ception when serializing

    The problem here comes from the fact that you are implementing
    IEnumerable<Ton T itself (in this case foo). You are better of creating a
    container class which implements IEnumerable<foo and then serialize that.

    Also, you don't need to the Serializable attribute when using XML
    Serialization.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <etienne.maitre @fr.michelin.co mwrote in message
    news:1194268331 .656393.198000@ 19g2000hsx.goog legroups.com...
    Hi,
    >
    I work against .NET 2.0 using C# 2.0. I am facing a problem trying to
    serialize the class foo (given below). Basically, foo can contain
    other foos in a list. When I try to create a serializer, using the
    test() method below, I get a superb StackOverflowEx ception. I googled
    for it but didn't found any answer to why I got this exception and how
    to serialize foo.
    >
    Any help accepted :-)
    >
    Thanks,
    >
    public void test()
    {
    XmlSerializer serializer = new XmlSerializer(t ypeof(foo));
    ...
    }
    >
    [Serializable]
    public class foo : IEnumerable<foo >
    {
    // ... some members...
    List<foog;
    >
    public foo()
    {
    g = new List<foo>();
    }
    >
    public IEnumerator<foo GetEnumerator()
    {
    foreach (foo element in g)
    {
    yield return element;
    }
    }
    >
    System.Collecti ons.IEnumerator
    System.Collecti ons.IEnumerable .GetEnumerator( )
    {
    return GetEnumerator() ;
    }
    >
    public void Add(Object e)
    {
    g.Add(e as foo);
    }
    }
    >

    Comment

    • not_a_commie

      #3
      Re: StackOverflowEx ception when serializing

      Your foo.g member is recursive. You need an object-reference type of
      serialization. The easiest fix for you would be to upgrade to WCF
      (.NET 3.0) and use the DataContractSer ializer with its support for
      object references.

      Comment

      Working...