1
votes

First of all, I really like protobuf-net for it's speed and it's ease of use. Now I'm developing a .NET class library and want to make some of my classes protobuf-net-ready without actually referencing it, because:

  1. I don't know which environment my clients will use, so I don't know which protobuf-net binary to choose
  2. I want to minimize the footprint size (maybe they won't need serialization at all)

As I understand, this can be done by writing .proto files manually. Could anyone explain me or give a link how to do this? Is there a way to generate .proto files from attribute description? And how will the client serialization/deserialization code look like?

1

1 Answers

1
votes

As I understand, this can be done by writing .proto files manually

Nope; this is not necessary, and won't help.

Let's suppose you have a type currently like:

[ProtoContract]
public class Foo {
    [ProtoMember(1)]
    public int Bar {get;set;}
    // ...
}

There are two options to do what you want:

  1. use the inbuilt platform attributes, for example:

    [DataContract]
    public class Foo {
        [DataMember(Order=1)]
        public int Bar {get;set;}
        // ...
    }
    

    or

    [XmlType]
    public class Foo {
        [XmlElement(Order=1)]
        public int Bar {get;set;}
        // ...
    }
    
  2. configure the model explicitly at runtime (before the model tries to work with Foo):

    RuntimeTypeModel.Default.Add(typeof(Foo), false).Add(1, "Bar");
    

    (the latter of course still requires you to talk to ProtoBuf at some point, but it does not require your POCO types to know about serialization)

Either of these should work.