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

发布于 2024-12-09 06:28:04 字数 2071 浏览 0 评论 0原文

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

I wanted to use protocolbuf .net version without attributes, with RuntimeTypeModel created via specific code. It worked pretty good till I hit the case, which was extracted in the case below. The referenced protobuf-net library's version is 2.0.0.447. What's wrong with this model's creation? Any clues?

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-12-16 06:28:04

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

initobj {type}
stloc.0

显然(咳嗽)对于值类型Descriptor它应该已经发出的错误时,

ldloca.s 0
initobj {type}

我会尽快部署它我已经检查过了,没有破坏任何东西。

Very simply; the library has (had) a bug, emitting

initobj {type}
stloc.0

when obviously (cough) for a value-type Descriptor it should have been emitting

ldloca.s 0
initobj {type}

I'll get this deployed as soon as I've checked I haven't destabilised anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文