ProtoBuf 不会反序列化具有接口的对象
ProtoBuf doesn't deserialize an object with interface
我有一个 TCP 套接字,我想使用 ProtoBuf 发送和接收对象。但我收到此错误:
Exception thrown: 'System.InvalidOperationException' in protobuf-net.dll
Type is not expected, and no contract can be inferred: Server.Packet.IMsg
我的界面:
public interface IMsg { }
我要发送和接收的对象之一:
[ProtoContract]
public class PacketPerson : IMsg
{
[ProtoMember(1)]
public string Name{ get; set; }
[ProtoMember(2)]
public string Country { get; set; }
}
我的套接字在获取完整缓冲区后尝试反序列化:
IMsg msg = Serialization.Desirialize(SocketMemoryStream);
反序列化:
public static IMsg Desirialize(MemoryStream ms) // socket buffer
{
ms.Position = 0;
return Serializer.Deserialize<IMsg>(ms);
}
yes, but I want to send multi-object and to identify them by using "Type type = packet.GetType();" Then use If statement "if (type == typeof(PacketPerson))"
我的建议(以作者身份发言):
[ProtoContract]
[ProtoInclude(1, typeof(PacketPerson))]
// future additional message types go here
class SomeMessageBase {}
[ProtoContract]
class PacketPerson : SomeMessageBase
{
[ProtoMember(1)]
public string Name{ get; set; }
[ProtoMember(2)]
public string Country { get; set; }
}
和 deserialize/serialize <SomeMessageBase>
。库将在这里以正确的方式处理所有的继承。在幕后,这将类似于(以 .proto 术语)实现:
message SomeMessageBase {
oneof ActualType {
PacketPerson = 1;
// ...
}
}
message PacketPerson {
string Name = 1;
string Country = 2;
}
您现在可以使用多态性或类型测试来确定运行时的实际类型。新的 switch
语法特别有用:
SomeMessageBase obj = WhateverDeserialize();
switch(obj)
{
case PacketPerson pp:
// do something person-specific with pp
break;
case ...:
// etc
break;
}
我有一个 TCP 套接字,我想使用 ProtoBuf 发送和接收对象。但我收到此错误:
Exception thrown: 'System.InvalidOperationException' in protobuf-net.dll
Type is not expected, and no contract can be inferred: Server.Packet.IMsg
我的界面:
public interface IMsg { }
我要发送和接收的对象之一:
[ProtoContract]
public class PacketPerson : IMsg
{
[ProtoMember(1)]
public string Name{ get; set; }
[ProtoMember(2)]
public string Country { get; set; }
}
我的套接字在获取完整缓冲区后尝试反序列化:
IMsg msg = Serialization.Desirialize(SocketMemoryStream);
反序列化:
public static IMsg Desirialize(MemoryStream ms) // socket buffer
{
ms.Position = 0;
return Serializer.Deserialize<IMsg>(ms);
}
yes, but I want to send multi-object and to identify them by using "Type type = packet.GetType();" Then use If statement "if (type == typeof(PacketPerson))"
我的建议(以作者身份发言):
[ProtoContract]
[ProtoInclude(1, typeof(PacketPerson))]
// future additional message types go here
class SomeMessageBase {}
[ProtoContract]
class PacketPerson : SomeMessageBase
{
[ProtoMember(1)]
public string Name{ get; set; }
[ProtoMember(2)]
public string Country { get; set; }
}
和 deserialize/serialize <SomeMessageBase>
。库将在这里以正确的方式处理所有的继承。在幕后,这将类似于(以 .proto 术语)实现:
message SomeMessageBase {
oneof ActualType {
PacketPerson = 1;
// ...
}
}
message PacketPerson {
string Name = 1;
string Country = 2;
}
您现在可以使用多态性或类型测试来确定运行时的实际类型。新的 switch
语法特别有用:
SomeMessageBase obj = WhateverDeserialize();
switch(obj)
{
case PacketPerson pp:
// do something person-specific with pp
break;
case ...:
// etc
break;
}