问题描述
假设我有以下基类:
[DataContract]
[Serializable]
public abstract class DimensionEntity
{
[DataMember(Order = 1)]
private readonly Date effectiveDatespan;
...
}
以及以下派生类:
[DataContract]
[Serializable]
public class ClearingSite : DimensionEntity
{
[DataMember(Order = 1)]
private readonly string code;
...
}
当我按如下方式序列化 ClearingSite
的一个实例时:
When I serialize an instance of ClearingSite
as follows:
model.Add(typeof(ClearingSite), true);
model.Serialize(ms, clearingSite);
可以看到只有ClearingSite
的code
成员被序列化了;基类的 effectiveDatespan
成员的值未序列化.
I can see that only the code
member of ClearingSite
is serialized; the value of the base class' effectiveDatespan
member is not serialized.
两个注意事项:
- 可以看到添加的
ProtoBuf.Meta.MetaType
的BaseType
成员设置为null
,导致effectiveDatespan
成员不被序列化;相反,如果我编译模型,它的BaseType
成员被正确设置为DimensionEntity
(尽管它后来失败了,因为成员是private readonly
和因此无法被编译模型访问); - 当然,我可以将
ClearingSite
声明为DimensionEntity
的已知类型,但我不明白为什么需要这样做:我没有序列化DimensionEntity
,我正在序列化(和反序列化)一个ClearingSite
,而且DataContractSerializer
不需要我添加KnownType
> 到DimensionEntity
如果我正在序列化一个ClearingSite
.
- I can see that the
BaseType
member of the addedProtoBuf.Meta.MetaType
is set tonull
, causing theeffectiveDatespan
member to not be serialized; if I compile the model, instead, itsBaseType
member is correctly set toDimensionEntity
(though it fails later on as the members areprivate readonly
and thus not accessible by a compiled model); - Of course I can declare
ClearingSite
as a known type ofDimensionEntity
, but I can't see why this would be required: I am not serializing aDimensionEntity
, I'm serializing (and de-serializing) aClearingSite
, and furthermore theDataContractSerializer
does not require me to addKnownType
toDimensionEntity
if I'm serializing aClearingSite
.
从 Marc 的其他答案来看,Protobuf 似乎需要 KnownType
(或 ProtoInclude
)属性才能获得所有重要的字段编号"(引用),但情况似乎并非如此,因为 CompiledModel
在没有 ProtoInclude
的情况下完全正常工作.
From other answers from Marc, it looks like Protobuf would require the KnownType
(or ProtoInclude
) attribute in order to get the "all-important field-number" (quote), but that seems to not be the case, as the CompiledModel
works totally fine without ProtoInclude
.
请注意,我正在努力仅使用 System.Runtime.Serialization
属性,因为我试图让我的对象模型不知道将来的序列化程序.
Note that I'm striving to use System.Runtime.Serialization
attributes only, as I'm trying to keep my object model unaware of serializers down the road.
推荐答案
ProtoInclude 或等效物,绝对是必需的.如果编译后的版本发生了奇怪的事情,那就是一个错误,我可以调查.
ProtoInclude, or an equivalent, is definitely required. If something is happening oddly with the compiled version, then that is a bug and I can investigate.
如果您不想向类型添加非 BCL 属性,可以在运行时完成:
If you don't want to add non-BCL attributes to your type, this can be done at runtime:
RuntimeTypeModel.Default[typeof(BaseType)]
.AddSubClass(.....);
(或类似的东西 - 我不在电脑前)
(or somethig like that - I'm not at a PC)
这篇关于Protobuf-net RuntimeTypeModel 不序列化基类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!