Protobuf with inheritance?

Is it possible to use protobuf with classes that inherit?

I want to do something like this

class Expr; class AddExpr : Expr; class CallFunc: Expr; class FunctionBody{ repeatable Expr expr; } 
+8
protocol-buffers
source share
1 answer

Not in the main implementation - you would like to use encapsulation instead.

However, if you use only protobuf-net as the first code, I cheat on 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 assume that there are additional details in the classes - β€œas is”, you can simply use an enumeration (which is well supported).

+10
source share

Source: https://habr.com/ru/post/651293/


All Articles