protobuf反序列化期间的Stackoverflow

时间:2014-07-16 11:19:19

标签: c# protobuf-net

我对protobuf-net 2.0.0.668有疑问。以下程序在反序列化时产生堆栈溢出:

[TestFixture]
internal class Program
{
    [Test]
    public void TestSerialize()
    {
        RuntimeTypeModel typeModel = TypeModel.Create();
        typeModel.Add(typeof(ISerializableType), true);
        typeModel[typeof(ISerializableType)].AddSubType(9, typeof(ITechnology));
        MetaType type = typeModel[typeof(ITechnology)].AddSubType(1, typeof(Gsm));
        type.UseConstructor = false;
        type.SetFactory("GetInstance");
        type = typeModel[typeof(ITechnology)].AddSubType(2, typeof(Lte));
        type.UseConstructor = false;
        type.SetFactory("GetInstance");
        typeModel.Add(typeof(Configuration), true);
        typeModel.Freeze();

        var expected = new Configuration { Technology = Gsm.GetInstance() };
        Configuration actual;
        using (var mem = new MemoryStream())
        {
            typeModel.SerializeWithLengthPrefix(mem, expected, typeof(Configuration), PrefixStyle.Fixed32, 0);
            mem.Position = 0;
            actual = (Configuration)typeModel.DeserializeWithLengthPrefix(mem, null, typeof(Configuration), PrefixStyle.Fixed32, 0);
        }

        actual.Technology.Should().BeSameAs(Gsm.GetInstance());
    }
}

使用以下实体定义:

public interface ISerializableType
{
}

public interface ITechnology
    : ISerializableType
{
}

public sealed class Gsm
    : ITechnology
{
    private static readonly Gsm Instance = new Gsm();

    private Gsm(){}

    #region Static Methods

    [Pure]
    public static Gsm GetInstance()
    {
        return Instance;
    }

    #endregion
}

public sealed class Lte
    : ITechnology
{
    private static readonly Lte Instance = new Lte();

    #region Static Methods

    [Pure]
    public static Lte GetInstance()
    {
        return Instance;
    }

    #endregion
}

[DataContract]
public sealed class Configuration
{
    [DataMember(Order = 3)]
    public ITechnology Technology;

    public Configuration()
    {
        Technology = new Lte();
    }
}

如果我删除配置文件或行" Technology = new Lte();",该示例确实有效,但是我不明白为什么这会导致protobuf无休止地进入首先是递归循环。

有人可以向我解释我做错了吗?

0 个答案:

没有答案