我有一个关于 c# 中 msgpack 序列化的问题。 我有一个基类和一个从基类派生的类,每个类都拥有属性,例如:
class Base
{
int PropertyA { get; set; }
}
class DerivedFromBase : Base
{
int PropertyB { get; set; }
}
此外,我还有一个第三类容器的对象,例如:
class Container
{
Base ContainerProperty { get; set; }
}
我的问题是,如果 ContainerProperty 的类型为 DerivedFromBase,MsgPack 仅序列化 PropertyA,而不序列化 PropertyB!我怎样才能实现 MsgPack 在内部将 ContainerProperty 识别为 DerivedFromBase?我是否必须编写自定义序列化器或者是否有“内置”方式?
谢谢。
最佳答案
使用属性MessagePackRuntimeType
让MsgPack在运行时分析类型以进行序列化:
class Container
{
[MessagePackRuntimeType]
Base ContainerProperty { get; set; }
}
有关更多详细信息,请参阅 msgpack-cli documentation on polymorphism
关于c# - 如何在 C# 中使用 MsgPack 序列化子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43045890/