是否可以使用 protobuf继承的类? 我想做这样的事情 class Expr;class AddExpr : Expr;class CallFunc: Expr;class FunctionBody{ repeatable Expr expr;} 不在核心实现中 – 您可能希望使用封装. 但是,如果你只使用
我想做这样的事情
class Expr;
class AddExpr : Expr;
class CallFunc: Expr;
class FunctionBody{
repeatable Expr expr;
}
不在核心实现中 – 您可能希望使用封装.
但是,如果你只使用protobuf-net作为代码优先,那我就解决它了:
[ProtoInclude(1, typeof(AddExpr))]
[ProtoInclude(2, typeof(CallFunc))]
[ProtoContract]
class Expr {}
[ProtoContract]
class AddExpr : Expr {}
[ProtoContract]
class CallFunc: Expr {}
[ProtoContract]
class FunctionBody{
private List<Expr> expressions;
[ProtoMember(1)]
public List<Expr> Expressions {
get { return expressions ?? (expressions = new List<Expr>()); }
}
}
当然,我假设在类中有一些额外的细节 – “按原样”你可以使用枚举(这是很好的支持).
