protobuf网UseImplicitZeroDefaults和枚举默认 [英] protobuf-net UseImplicitZeroDefaults and Enum defaults

查看:313
本文介绍了protobuf网UseImplicitZeroDefaults和枚举默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用protobuf网,但无法理解UseImplicitZeroDefaults我们现在禁用的自定义RuntimeTypeModel。我们最初使用的是默认RuntimeTypeModel但我们注意到,即使是被指定一个默认值布尔属性没有被克隆,即默认值=真实的,但如果设置为false克隆的财产将永远是正确的。



我们通过创建使我们能够UseImplicitZeroDefaults设置为false自定义RuntimeTypeModel解决了这个。但这种设置为false导致下面的错误;

  ProtoBuf.ProtoException:没有线值映射到枚举

请注意,我们的一些枚举是基于不为零,这可能会导致一个问题?我们如何可以克隆/序列化布尔属性和枚举(基于非零和零混合物)



编辑:我使用的一些信息在这里找到:的 protobuf网枚举系列化并可以报告:

  [ProtoMember(10),默认值(SiteType.Partition)
公共SiteType类型{搞定;组; }



仍然导致了无丝价值的错误。

  [ProtoMember(10,IsRequired = TRUE)] 
公共SiteType类型{搞定;组; }



仍然导致了无丝价值的错误。

 公共枚举SiteType 
{
错误= 0,
...

这工作,但最好我们要离开我们的枚举干净结果
也许一个更清洁的方式来指定默认值:

  [默认值(SiteType.Server)
公共枚举SiteType
{
服务器= 1,
猴子= 2
...


解决方案

我们通过指定一个默认的枚举任何非零基于枚举解决了这个问题。我们指定在类的构造函数的默认被序列化。这是迄今为止最整洁的解决方案,不需要任何额外的protobuf网属性。



此外,这是有道理的明确设置为基础的非默认值为零枚举属性。


We're trying to use protobuf-net but having trouble understanding UseImplicitZeroDefaults which we now disable in a custom RuntimeTypeModel. We initially used the default RuntimeTypeModel but noticed boolean properties were not being cloned even though a DefaultValue was being specified, i.e. DefaultValue=true but when set to false the cloned property would always be true.

We resolved this by creating a custom RuntimeTypeModel which has allowed us to set UseImplicitZeroDefaults to false. But setting this to false is causing the following error;

ProtoBuf.ProtoException: No wire-value is mapped to the enum

Note that some of our enums are non-zero based, could this be causing an issue? How can we clone/serialize boolean properties and enums (mixture of non-zero and zero based)?

Edit: I used some of the information found at: protobuf-net enum serialization and can report:

[ProtoMember(10), DefaultValue(SiteType.Partition)]
public SiteType Type { get; set; }

Still results in the "No wire-value" error.

[ProtoMember(10, IsRequired = true)]
public SiteType Type { get; set; }

Still results in the "No wire-value" error.

public enum SiteType
{
    Error = 0,
    ...

This works but ideally we would like to leave our enum clean.
Perhaps a cleaner way to specify the default value:

[DefaultValue(SiteType.Server)]
public enum SiteType
{
    Server = 1,
    Monkey = 2
    ...

解决方案

We resolved this issue by specifying a default enum for any non-zero based enums. We specified the default in the constructor of the class being serialized. This was by far the tidiest solution and didn't require any additional protobuf-net attributes.

Plus, it made sense to explicitly set a default value for non-zero based enum properties.

这篇关于protobuf网UseImplicitZeroDefaults和枚举默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆