在Protobuf-net中装饰中级类

时间:2014-02-14 15:39:57

标签: c# protobuf-net

在protobuf-net中,哪个基类应该装饰?该类是直接子类还是最远的基类?或两者兼而有之?

[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 { }

1 个答案:

答案 0 :(得分:1)

每个预期子类型的直接父类,而不是祖先。

所以:BaseClass需要声明Intermediary,而Intermediary需要声明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 { }