创建的RuntimeTypeModel在反序列化复杂对象期间失败

本文关键字:对象 失败 复杂 反序列化 RuntimeTypeModel 创建 | 更新日期: 2023-09-27 18:11:05

我想使用没有属性的协议。net版本,通过特定的代码创建RuntimeTypeModel。它工作得很好,直到我碰到了箱子,它在下面的例子中被提取出来。参考的protobuf-net库版本为2.0.0.447。这个模型的创建出了什么问题?有线索吗?

public class ProtoBufFailingTest
{
    public abstract class Message
    {
    }
    public class SomeMessage : Message
    {
        public readonly Descriptor Desc;
        public SomeMessage(Descriptor desc)
        {
            Desc = desc;
        }
    }
    public struct Descriptor
    {
        public readonly Event EventData;
        public Descriptor(Event eventData)
        {
            EventData = eventData;
        }
    }
    public abstract class Event
    {
    }
    public class SomeEvent : Event
    {
        public int SomeField;
    }
    [Test]
    public void FailingTest( )
    {
        var model = TypeModel.Create();
        // message hierarchy
        {
            var messages = model.Add(typeof(Message), true);
            messages.AddSubType(1, typeof(SomeMessage));
            model[typeof(SomeMessage)].UseConstructor = false;
        }
        // events hierarchy
        {
            var events = model.Add(typeof (Event), true);
            events.AddSubType(1, typeof (SomeEvent));
            model[typeof (SomeEvent)].UseConstructor = false;
        }
        // descriptor
        var eventDescriptorModel = model.Add(typeof(Descriptor), true);
        eventDescriptorModel.UseConstructor = false;
        var typeModel = model.Compile();
        const PrefixStyle prefixStyle = PrefixStyle.Base128;
        const int testValue = 5;
        using (var ms = new MemoryStream())
        {
            typeModel.SerializeWithLengthPrefix(ms, new SomeMessage(new Descriptor(new SomeEvent { SomeField = testValue })), null, prefixStyle, 0);
            ms.Seek(0, SeekOrigin.Begin);
            // fails here
            var message = (SomeMessage)typeModel.DeserializeWithLengthPrefix(ms, null, typeof(Message), prefixStyle, 0);
            Assert.AreEqual(testValue, ((SomeEvent)message.Desc.EventData).SomeField);
        }
    }
}

创建的RuntimeTypeModel在反序列化复杂对象期间失败

非常简单;库有一个错误,发出

initobj {type}
stloc.0

明显(咳)对于值类型Descriptor,它应该发出

ldloca.s 0
initobj {type}

一旦我检查了我没有破坏任何东西的稳定,我就会把它部署。

相关文章: