1

is it possible to create a generic copy constructor based on protobuf-net? Something like:

   public class Person
   {
      public Int32 Id { get; set; }

      public String FirstName { get; set; }

      public Int32 Age { get; set; }

      public String Name { get; set; }
   }


   public static void DeepCopyCosntructor<T>(T source, T target)
   {
      // copy all properties by protobuf-net
   }

I want to avoid reflection, but I don't know how to fill the properties of target without recreating a new object.

Franki1986
  • 1,320
  • 1
  • 15
  • 40

1 Answers1

1

The issue to consider here is the "merge" semantics as defined by the protobuf specification. You'd be fine for basic properties, but for collections and dictionaries, the default behaviour is to add, not replace. So, you'd need to add the usual protobuf-net attributes, and make sure that every collection is set to overwrite:

[ProtoMember(n, OverwriteList = true)]

Again, I'm not sure this is the best use-case for protobuf-net, but: a merge can be done by passing in target; so with the v1 API:

using (var ms = new MemoryStream())
{
    Serializer.Serialize<T>(ms, source);
    ms.Position = 0;
    target = Serializer.Merge<T>(ms, target);
}

or in the v2 API:

using (var ms = new MemoryStream())
{
    RuntimeTypeModel.Default.Serialize(ms, source);
    ms.Position = 0;
    target = RuntimeTypeModel.Default.Deserialize(ms, target, typeof(T));
}

Note that in both cases the target = is primarily to handle the scenario where target is initially null.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for that! One question to the 'OverwriteList': I would like to use protobuf-net for all my classes, and because I don't want to always specify the attributes I would like to do it over 'RuntimeTypeModel'. So is it worse to always set 'OverwriteList' to true? – Franki1986 Jan 02 '18 at 10:32
  • @Franki1986 not "worse" - just different; it will have the effect of meaning you can't use "append===merge with append", which is the behaviour specified by Google's implementation. If you don't care about that: have fun – Marc Gravell Jan 02 '18 at 11:19