Question

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 { }
Était-ce utile?

La solution

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 { }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top