Home > .net > ProtoBuf.net – Handling optional primitives

ProtoBuf.net – Handling optional primitives

January 27, 2010 Leave a comment Go to comments

If you are dealing with protos containing optional primitives, it’s not immediately obvious how you’d deal with them in C#.

For example, if the proto contains an optional int and you generated the C# class, the results would as follows:

optional int32 bar_ukey = 13;

>protogen -i:BarTest.proto -o:BarTestProto.cs

private int _bar_ukey =default(int);
[ProtoBuf.ProtoMember(13, IsRequired = false, Name=@"bar_ukey", DataFormat = ProtoBuf.DataFormat.TwosComplement)][System.ComponentModel.DefaultValue(default(int))]
public int bar_ukey
get { return _bar_ukey; }
set { _bar_ukey = value; }

As you can see, the highlighted default value doesn’t take the optional nature of the field into account.

To fix this, protogen needs to be run with detectMissing argument as follows:
protogen -i:BarTest.proto -o:BarTestProto.cs -p:detectMissing
which results in the property getting generated as

private int? _bar_ukey;
[ProtoBuf.ProtoMember(13, IsRequired = false, Name=@"bar_ukey", DataFormat = ProtoBuf.DataFormat.TwosComplement)]
public int bar_ukey
get { return _bar_ukey?? default(int); }
set { _bar_ukey = value; }
public bool bar_ukeySpecified
get { return _bar_ukey != null; }
set { if (value == (_bar_ukey== null)) _bar_ukey = value ? bar_ukey : (int?)null; }
private bool ShouldSerializebar_ukey() { return bar_ukeySpecified; }
private void Resetbar_ukey() { bar_ukeySpecified = false; }

Side note: Also tucked away in the -p template property observable and preObservable options for the observable pattern.

Categories: .net
  1. No comments yet.
  1. March 15, 2010 at 8:16 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: