Tag: protocol buffers

ProtoInclude属性意味着什么(在protobuf-net中)

在ProtoBuf-Net实现中, ProtoInclude属性意味着什么,它有什么作用? 一个例子将不胜感激。 我在这篇文章中看到了它,我不确定它是做什么的。 这个例子是: [Serializable, ProtoContract, ProtoInclude(50, typeof(BeginRequest))] abstract internal class BaseMessage { [ProtoMember(1)] abstract public UInt16 messageType { get; } } [Serializable, ProtoContract] internal class BeginRequest : BaseMessage { [ProtoMember(1)] public override UInt16 messageType { get { return 1; } } } 另外,有没有办法使用protogen工具生成这种inheritance?

protobuf-netinheritance

Marc在stackoverflow上提到,在protobuf-net的v2中可以使用ProtoInclude属性(或类似方法)来序列化/反序列化类层次结构,而无需在基类中指定每个子类型。 这实现了吗? 我们有一个可以在外部库中派生的插件接口,因此无法知道派生类型是什么。 我们可以在类型之间保持唯一的编号,但我在网上找不到任何示例,缺少使用ProtoInclude属性,需要指定子类型。 如果我不知道子类是什么,我将如何使用protobuf-net实现inheritance?

使用protobuf-net反序列化未知类型

我有2个网络应用程序,应该相互发送序列化的protobuf-net消息。 我可以序列化对象并发送它们,但是, 我无法弄清楚如何反序列化接收的字节 。 我尝试使用它反序列化,并且失败并出现NullReferenceException。 // Where “ms” is a memorystream containing the serialized // byte array from the network. Messages.BaseMessage message = ProtoBuf.Serializer.Deserialize(ms); 我在包含消息类型ID的序列化字节之前传递一个标头,我可以在一个巨大的switch语句中使用它来返回预期的sublcass Type。 使用下面的块,我收到错误:System.Reflection.TargetInvocationException —> System.NullReferenceException。 //Where “ms” is a memorystream and “messageType” is a //Uint16. Type t = Messages.Helper.GetMessageType(messageType); System.Reflection.MethodInfo method = typeof(ProtoBuf.Serializer).GetMethod(“Deserialize”).MakeGenericMethod(t); message = method.Invoke(null, new object[] { ms }) […]