做什么:"没有线值映射到枚举foo位置16"错误意味着什么?

jSt*_*aff 3 protocol-buffers protobuf-net

"没有线值映射到enum foo位置16"

这个错误信息是什么意思?

  • i.)什么是线值?

  • ii.)相对的位置是什么?

在我的特殊情况下,我有:

附加信息:没有wire-value映射到位置16的枚举System.Drawing.ContentAlignment.0

在我知道的整数0的枚举中没有表示.我想只要知道错误报告的含义就会让我更好地了解如何解决这个问题.

Mar*_*ell 6

"线值"是它将"在线上"使用的二进制表示,即在底层流中.在这种情况下,它已经使用的事实0意味着枚举的值是0,但它不期望0,大多数可能是因为没有定义具有值为0的枚举成员.如果有,它,它会使用这个名字,而不是System.Drawing.ContentAlignment.0.protobuf-net正在尝试应用枚举规则,强制这些值有意义(并且还可以选择允许您在枚举值和线值之间定义映射,对于某些具有外部数据的场景)

有多种方法可以避免这个问题:

  • 不要尝试序列化不存在的枚举值(或者:如果控制枚举,则添加缺失值)
  • 当你想表示那个时,使用Nullable<SomeEnum>(SomeEnum?)来序列化一个缺少None值的枚举
  • 使用"shim"属性将类型公开为int(或任何基础类型),以便protobuf-net永远不会尝试应用枚举规则:

    public SomeEnum Foo {get;set;} // the actual member
    [ProtoMember(42)]
    private int FooSerialized { // only exists to help protobuf-net ignore the
        get { return (int)Foo; }// invalid values that the member might contain
        set { Foo = (SomeEnum)value; }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • RuntimeTypeModel通过设置EnumPassthru为,手动配置模型并告诉protobuf-net忽略该成员的枚举处理true

  • 用枚举声明标记[Flags],这将使protobuf-net EnumPassThru自动启用