如何为具有长值的枚举解析 'Arithmetic operation resulted in an overflow.'
How to resolve 'Arithmetic operation resulted in an overflow.' for enums with long values
假设我有一个枚举
public enum PricingFlags : long
的值为 100000000000100001111111000010001_2 或 4296080913。基本上是标志的集合,例如
[Description("Aggregate")]
Aggregate = 1L << 7,
逻辑或运算在一起。我使用的是 2.3.1 版本。
我尝试在属性
中添加 EnumPassthru
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic, EnumPassthru = true)]
或者在 protobuf 规范中,
RuntimeTypeModel.Default[typeof(PricingFlags)].EnumPassthru = true;
谷歌搜索后,我发现 https://github.com/protobuf-net/protobuf-net/issues/219 告诉我它有可能起作用,但它没有起作用。我做错了什么吗?
编辑:
我最终将 属性 更改为具有支持字段并使其成为原型成员并忽略了 属性 本身。
[ProtoMember(9021, Name = "Flags")]
private long _flagsValue;
...
[ProtoIgnore]
public PricingFlags Flags
{
get => (PricingFlags)_flagsValue;
set => _flagsValue = (long)value;
}
我今天的建议是:声明一个影子 属性(也许是私有的),将数据公开为 long,在您的代码中进行转换。
V2 枚举代码对此有点难看。我在 V3 中打破了这个(基本上所有枚举都变成了原生 passthrus)所以我将添加这些作为 V3 的测试场景。
假设我有一个枚举
public enum PricingFlags : long
的值为 100000000000100001111111000010001_2 或 4296080913。基本上是标志的集合,例如
[Description("Aggregate")]
Aggregate = 1L << 7,
逻辑或运算在一起。我使用的是 2.3.1 版本。
我尝试在属性
中添加 EnumPassthru[ProtoContract(ImplicitFields = ImplicitFields.AllPublic, EnumPassthru = true)]
或者在 protobuf 规范中,
RuntimeTypeModel.Default[typeof(PricingFlags)].EnumPassthru = true;
谷歌搜索后,我发现 https://github.com/protobuf-net/protobuf-net/issues/219 告诉我它有可能起作用,但它没有起作用。我做错了什么吗?
编辑:
我最终将 属性 更改为具有支持字段并使其成为原型成员并忽略了 属性 本身。
[ProtoMember(9021, Name = "Flags")]
private long _flagsValue;
...
[ProtoIgnore]
public PricingFlags Flags
{
get => (PricingFlags)_flagsValue;
set => _flagsValue = (long)value;
}
我今天的建议是:声明一个影子 属性(也许是私有的),将数据公开为 long,在您的代码中进行转换。
V2 枚举代码对此有点难看。我在 V3 中打破了这个(基本上所有枚举都变成了原生 passthrus)所以我将添加这些作为 V3 的测试场景。