When does the ProtoEnum attribute need to be used for protobuf-net?

Viewed 784

The protobuf-net includes a [ProtoEnum] attribute that can be used to decorate members of an enum. When and how should this be used?

In normal usage scenarios, enum member serialization seems to work just fine (as long as the enum itself has a [ProtoContract] decorating it).

1 Answers

The only times you need this are:

  1. when you're working .proto => C# or C# => .proto, and you want the name in the .proto to be different to the actual enum declaration
  2. when you want to map the numbers on the wire to numbers in your type model for some reason, i.e. the enum value that is 12 on the wire (to other users of the same data) would more usefully be 20 to your app somehow

I usually strongly discourage the second option; it makes things too complex, and it means it needs to apply an extra bit of logic in both directions. This is especially true now that "proto3" changed the expected behavior of unrecognized values; in "proto2", this was meant to indicate an error, but in "proto3" you're meant to just take the value. You can't do this if you're applying a map.

Related