Does anyone have a working example where protobuf-net can be used as a drop-in replacement for BinaryFormatter? Is that even possible?
Actually I just need a serializer for one message type which looks like
public class A {
public B[] Bs { get;set; }
public C[] Cs { get;set; }
}
All types are defined in a different assembly and have a lot of properties.
Is there an option to automatically generate proto contracts with all public properties included, for class A and other used types (B, C), so something like
var formatter = ProtoBuf.Serializer.CreateFormatter<A>()
just works?
Firstly, protobuf-net is not intended to be, and does not claim to be, a 100% drop in replacement for
BinaryFormatter. It has slightly different features.Would you be content to do a little reflection? Basically, there is support for
ImplicitFields, but currently this is only available via[ProtoContract], and cannot be done conveniently viaRuntimeTypeModel, which is a bit of a pain, and is on my list. Although, I should point out that I consider implicit-fields to be a bit risky, and it should only be done if you know the DTO innards won’t change! But: to answer your question, you could iterate over the types you expect, and add them to the model manually:Note that the use of
IFormatterhere is entirely unnecessary; you could also useRuntimeTypeModel.Default.Serialize(...)orSerializer.Serialize<T>(...).As a footnote: I would advise defining the models more … repeatably. For example: