In protobuf-net, which base class should be decorated? The class being directly subclassed or the furthest base class? Or both?

[ProtoContract]
[ProtoInclude(42, typeof(Derived))] // Here?
public abstract class BaseClass { }

[ProtoContract]
[ProtoInclude(42, typeof(Derived))] // Or Here?
public abstract class Intermediary : BaseClass { }

[ProtoContract]
public class Derived : Intermediary { }
有帮助吗?

解决方案

The immediate parent of each expected sub-type, not the ancestor.

So: BaseClass needs to declare Intermediary, and Intermediary needs to declare Derived:

[ProtoContract]
[ProtoInclude(42, typeof(Intermediary))]
public abstract class BaseClass { }

[ProtoContract]
[ProtoInclude(42, typeof(Derived))]
public abstract class Intermediary : BaseClass { }

[ProtoContract]
public class Derived : Intermediary { }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top