Search code examples
protobuf-net

How to resolve 'Arithmetic operation resulted in an overflow.' for enums with long values


Lets say I have a enum

public enum PricingFlags : long

that has a value of 100000000000100001111111000010001_2 or 4296080913. Basically a collection of flags like

[Description("Aggregate")]
Aggregate = 1L << 7,

logical OR'ed together. I am using version 2.3.1.

I tried adding EnumPassthru in attribute

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic, EnumPassthru = true)]

or in protobuf specification,

 RuntimeTypeModel.Default[typeof(PricingFlags)].EnumPassthru = true;

after googling I found https://github.com/protobuf-net/protobuf-net/issues/219 that told me there is a chance that it should work, but it did not. Am I doing something wrong?


Edit:

I ended up changing the property to have a backing field and making it a proto member and ignoring the property itself.

[ProtoMember(9021, Name = "Flags")]
private long _flagsValue;
...
[ProtoIgnore]
public PricingFlags Flags
{
    get => (PricingFlags)_flagsValue;
    set => _flagsValue = (long)value;
}

Solution

  • My advice for today would be: declare a shadow property (perhaps private) that exposes the data as a long, doing the cast in your code.

    The V2 enum code is kinda ugly for this. I'm smashing this in V3 (basically all enums become native passthrus) so I will add these as test scenarios for V3.