gpt4 book ai didi

c# - 意外的子类型 : MyNamespace. MyInheritedClass

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:56 25 4
gpt4 key购买 nike

在 protobuf-net 中序列化时出现异常 Unexpected sub-type: UnnamedGameServer.TrapInstance

这是代码:

class test
{
void testMethod(PacketNewTrapResponse packet)
{
using (var stream = new MemoryStream())
{
Serializer.SerializeWithLengthPrefix<PacketNewTrapResponse>(stream, (PacketNewTrapResponse)packet, PrefixStyle.Base128);
}
}
}

[ProtoContract]
public class MapTrap
{
[ProtoMember(1)]
public IntegerVector2 Position;
[ProtoMember(2)]
public int TrapServerID;
[ProtoMember(3)]
public int LocationID;
}

[ProtoContract, ProtoInclude(1, typeof(MapTrap))]
class TrapInstance : MapTrap
{
public TrapInstance(TrapProperties trap, SessionCharacter session, int serverTrapId, int locationId, IntegerVector2 position)
{
TrapServerID = serverTrapId;
Trap = trap;
Position = position;
LocationID = locationId;
OwnerOfTrap = session;
LocationID = locationId;
Position = position;
}

public SessionCharacter OwnerOfTrap { get; set; }
public TrapProperties Trap { get; set; }
}

最佳答案

需要告知基类有关子类的信息,而不是相反。从子类中寻找基类微不足道,因为它在运行时很容易获得。

[ProtoContract, ProtoInclude(5, typeof(TrapInstance))]
public class MapTrap {...}

[ProtoContract]
class TrapInstance : MapTrap {...}

关于c# - 意外的子类型 : MyNamespace. MyInheritedClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com