在反序列化复杂对象期间,Created RuntimeTypeModel失败

时间:2011-10-10 21:37:32

标签: c# protobuf-net

我想使用不带属性的protocolbuf .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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

initobj {type}
stloc.0

明显(咳嗽)时,值类型Descriptor应该一直在发光

ldloca.s 0
initobj {type}

一旦我检查了我没有破坏任何稳定性,我会立即部署。