创建的 RuntimeTypeModel 在反序列化复杂对象期间失败
我想使用不带属性的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常简单;该库有一个错误,
当显然(咳嗽)对于值类型
Descriptor
它应该已经发出的错误时,我会尽快部署它我已经检查过了,没有破坏任何东西。
Very simply; the library has (had) a bug, emitting
when obviously (cough) for a value-type
Descriptor
it should have been emittingI'll get this deployed as soon as I've checked I haven't destabilised anything.