How to set wire format on a property without changing type definition?

StackOverflow https://stackoverflow.com/questions/23091350

  •  04-07-2023
  •  | 
  •  

سؤال

This type is defined in an assembly where I cannot add a reference to protobuf-net:

[StructLayout(LayoutKind.Sequential)]
[XmlType] // for XML or protobuf-net serialization
public struct PointI
{
    public PointI(int x, int y) { X = x; Y = y; }

    [XmlElement(Order = 1)]
    public int X;
    [XmlElement(Order = 2)]
    public int Y;
    ...
}

How can I use ProtoBuf.Meta.RuntimeTypeModel.Default to specify zig-zag storage to optimize output size?

هل كانت مفيدة؟

المحلول

var metaType = RuntimeTypeModel.Default.Add(typeof(PointI), true);
metaType[1].DataFormat = metaType[2].DataFormat = ProtoBuf.DataFormat.ZigZag;

That do ? In other news: mutable structs... bleugh...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top