自定义类导致.NET的XML序列化程序中的堆栈溢出

时间:2011-11-30 15:44:47

标签: c# exception-handling dictionary xml-serialization stack-overflow

好吧,帖子太长了,并且包含太多的自我修正,所以我从头开始重写它。如果您想回读,请查看更改日志。

可以在此处找到最新版本的代码:http://pastebin.com/KMHVb5gA 我出于某种原因在system.XML.dll中出现溢出;它没有堆栈跟踪,只是停止执行。

我不知道为什么它会溢出,但我知道它在调用Save()

后发生
            public bool save(string filename) //causes stack overflow when savenode is IEnumerable (and otherwise it does nothing).
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
                System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.OpenOrCreate);
                serializer.Serialize(fstream, innerdict);
                fstream.Flush();
                fstream.Close();
                return true;
            }

我对制作自己的IEnumerable课程完全不熟悉,所以如果你看到一些明显的错误,请告诉我!

调用代码如下所示:

        Console.WriteLine("Commencing XML persistency test");
        CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary(logger); //Works!!
        persistence.Add("test", "testvaluefennecs");
        Console.WriteLine(persistence["test"].obj);
        foreach (var snode in persistence)
        {
            Console.Write("Contents: " + snode.obj);
        }
        persistence.save("test.xml");
        persistence.load("test.xml");
        if (persistence["test"].obj != "testvaluefennecs")
        {
            logger.logerror(new Exception("XML test failed!! Expected 'testvaluefennecs', got: " + persistence["test"].obj));
        }
        else
            Console.WriteLine("XML test success!");

输出如下:

Commencing XML persistency test
[17:54:09] info: Initialized new XMLPersistence dictionary
New node: test
[17:54:09] Notice: Adding new dictionary item: test
testvaluefennecs
Contents: testvaluefennecs
Process is terminated due to StackOverflowException.

有人有想法吗?欢迎任何建议!我完全坚持这个!

[编辑] 刚刚找到溢出的确切线,就是这样:

XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));

1 个答案:

答案 0 :(得分:0)

我知道了!我的一个朋友向我指出了这一点,我必须补充一点,JonB也帮助了很多!

问题是savenode是IEnumerable。我以为我需要那个,但事实证明我不需要! 序列化程序溢出的原因是因为使savenode可枚举,将List转换为列表数组(它无法序列化!)

诀窍就是再次将savenode变为非IEnumerable,并且一切正常!

现在找到其他新错误并在更短的时间内拍打它们。 有时,程序员就像扮演超级肉食男孩;在DAYS你被困在同一水平但是当你最终钉住它时,你正在用你的快乐唤醒neighboars。 (ups lol)

感兴趣的人的新版代码:http://pastebin.com/7xQUMAUn

当前的调用代码,工作:

        Console.WriteLine("Commencing XML persistency test");
        CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary(logger); //Works!!
        persistence.Add("test", "testvaluefennecs");
        persistence["test"].Add("test", "fennecs");
        Console.WriteLine(persistence["test"].obj);
        foreach (var snode in persistence)
        {
            Console.Write("Contents: " + snode.obj);
            foreach (var item in snode.childnodes)
            {
                Console.Write("Contents: " + snode.obj);
            }
        }
        Console.WriteLine("\nSaving dictionary");
        persistence.save("test.xml");
        persistence = null; Console.WriteLine("Nullified dictionary");
        persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary();
        persistence.load("test.xml");
        Console.WriteLine("Loaded dictionary");
        if (persistence["test"].obj != "testvaluefennecs")
        {
            logger.logerror(new Exception("XML test failed!! Expected 'testvaluefennecs', got: " + persistence["test"].obj));
        }
        else
            Console.WriteLine("XML test success!");

代码可以和你在一起!