递归异常



我在这段代码中遇到了递归问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using ProtoBuf;
namespace ConsoleApplication4
{
[Serializable]
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class GeneralWrapper<T> where T : new()
{
    public GeneralWrapper()
    {
        Datas = new T();
    }
    public T Datas { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        List<Document> _documents = new List<Document>();
        for (int i = 0; i < 100000; i++)
        {
            Document _document = new Document()
            {
                DocumentName = "asdadsf"
            };
            _documents.Add(_document);
        }
        BinaryFormatter _formatter = new BinaryFormatter();
        FileStream fs = new FileStream
            ("Person1.bin", FileMode.OpenOrCreate);
        ProtoBuf.Serializer.Serialize(fs, _documents);
        fs.Close();
        // Deserialize.
        fs = new FileStream
            ("Person1.bin", FileMode.OpenOrCreate);
        List<Document> _document22 = ProtoBuf.Serializer.Deserialize<List<Document>>(fs);
        fs.Close();
    }
}
[ProtoContract]
public class Document
{
    public Document()
    {
        _count = 234234924;
        Section = new Section();
        Section.SectionName = "sdfasd";
    }
    [ProtoMember(1)]
    public string DocumentName { get; set; }
    [ProtoMember(2)]
    Dictionary<string, List<string>> Hello { get; set; }
    [ProtoMember(3, AsReference=true)]
    public Section Section { get; set; }
    [ProtoMember(4)]
    private string _sectionName;
    [ProtoMember(5)]
    public string SectionName
    {
        get
        {
            return Section.SectionName;
        }
        set
        {
            _sectionName = value;
            Section.SectionName = _sectionName;
        }
    }
    public int _count;
    public int Count
    {
        get
        {
            return _count;
        }
    }
}
[Serializable]
[ProtoContract]
public class Section
{
    public Section()
    {
        Section1 = new SectionInner(this);
        Hellos = new List<GeneralWrapper<List<string>>>();
        GeneralWrapper<List<string>> _hello = new GeneralWrapper<List<string>>();
        _hello.Datas.Add("hello");
        Hellos.Add(_hello);
        DHello = new Dictionary<string, List<Section>>();
        if (!DHello.ContainsKey("asdf"))
        {
            List<Section> _dhello1 = new List<Section>();
            _dhello1.Add(this);
            DHello.Add("asdf", _dhello1);
        }
    }
    [ProtoMember(1, AsReference=true)]
    public SectionInner Section1 { get; set; }
    [ProtoMember(2)]
    public string SectionName { get; set; }
    [ProtoMember(3, AsReference=true)]
    public Dictionary<string, List<Section>> DHello { get; set; }
    List<GeneralWrapper<List<string>>> Hellos { get; set; }
}
[Serializable]
[ProtoContract]
public class SectionInner
{
    public SectionInner(Section section)
    {
        Section = section;
    }
    [ProtoMember(1, AsReference=true)]
    public Section Section { get; set; }
}

}

很明显,我一开始就让代码非常递归,因为这和我真正的项目是一样的。问题似乎是:

 Dictionary<string, List<Section>>

如果没有向该字典中添加任何内容,则所有内容都可以串行化。如果使用某个键将列表添加到字典中,则会发生递归。

protobuf-net中是否支持此代码/语法?:

 Dictionary<string, List<Section>>

我需要把List放在一个外部包装类中吗?比如:

 Dictionary<string, Wrapper<List<Section>>>

谢谢你的帮助。我是protobuf网络的新手。

首先,我必须注意,在构造函数中执行这么多设置不是一个好主意。如果这表示您的实际代码,那么您可能希望在反序列化([ProtoContract(SkipConstructor=true)])期间跳过构造函数。如果只是说明性的,那就好了。

是的,字典和列表之类的东西是受支持的,但是*直接嵌套列表是不受支持的——所以List<List<...>>目前不可用。您可能会用Dictionary<TKey,List<...>>来处理它,因为它的键-值-路径已经充当了在中间的包装器。

再递归:protobuf-net支持许多递归场景,但这是对形式规范的扩展。因此,您需要显式启用它,并注意:互操作这个场景并不容易,因为没有正式的规范;但是:[ProtoMember(n, AsReference=true)]能够对单个成员进行对象跟踪。请注意,所有使用此对象的地方都必须标记为这样,否则它们将使用树序列化。

关于"为什么不直接支持递归"-因为:protobuf(正式规范)的行为与这里的大多数序列化程序类似,是一个序列化程序。注意:XmlSerializer、JavascriptSerializer和DataContractSerializer(在默认模式下)也是树序列化程序,如果给定递归结构,它们将爆炸。这很正常。protobuf-net在一些场景中特意允许这样做,但默认情况下无法启用,因为它需要不同的数据布局,这违背了跨平台数据规范的目的。

如果我遗漏了你的任何问题,请说。

最新更新