protobuf 继承? [英] protobuf with inheritance?
本文介绍了protobuf 继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将 protobuf 与继承的类一起使用?
Is it possible to use protobuf with classes who inherit?
我想做这样的事情
class Expr;
class AddExpr : Expr;
class CallFunc: Expr;
class FunctionBody{
repeatable Expr expr;
}
推荐答案
不在核心实现中 - 您可能希望使用封装.
Not in the core implementation - you would want to use encapsulation instead.
但是,如果您使用只是protobuf-net,作为代码优先,我绕过它:
However if you are using just protobuf-net, as code-first, I hack around it:
[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>()); }
}
}
当然,我假设类中有一些额外的细节——按原样"你可以只使用枚举( 得到了很好的支持).
Of course, I'm assuming there is some additional detail in the classes - "as is" you could just use an enum (which is well-supported).
这篇关于protobuf 继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文