4
votes

I have a .NET type, which is attributed with neither ProtoContract nor DataContract. In addition, not all of its state need to be proto-serialized. Can I define a .proto file for it, but at the same time use some kind of a Serializer to serialize it as though it is attributed with ProtoContract?

Thanks.

1

1 Answers

6
votes

As a third option, [XmlType] and [XmlElement(Order=n)] can be used... but I don't think that is what you mean ;p

In "v2", this is indeed possible. You don't need to define a .proto - you can simply tell it what to do at runtime, for example:

var model = TypeModel.Create();
model.Add(typeof(SomeType)).Add("Foo", "Bar", "Blip");

now store model somewhere (and re-use it), and use model.Serialize(...) and model.Deserialize(...). The above configures SomeType to serialize .Foo (as field 1), .Bar (as field 2) and .Blip (as field 3). There are of course many more ways of doing this for more fine-grained control.

It will generate (as first needed) a serializer (via IL emit, so very fast) that works with your types as expected.

There is a downloadable "v2" dll, but it needs updating - I have done a lot of fixes in the last few weeks. I will try to refresh this dll later today, or you can build from code.