Error while using ProtoBuf-Net with flags enum

前端 未结 1 544
臣服心动
臣服心动 2021-01-05 14:51

While using ProtoBuf-Net and serializing an enum property, where the enum is set to [FlagsAttribute], I received the following error message when serializing an enum value c

相关标签:
1条回答
  • 2021-01-05 15:39

    Update : this is now fixed in r274; you would use:

    [ProtoMember(12, DataFormat = DataFormat.TwosComplement)]
    public MyEnum MyValue {get;set;}
    

    Ultimately the protocol buffers wire format doesn't provide any scope for [Flags] enums - it enforces enum values against the discreet set. I could allow this easily enugh, but:

    • I'd probably have to disable enum mappings in this case, or do a lot of ugly bit- matching work
    • it would not be strictly compatible

    An easier way of doing this may be to do a shim in your code:

    public MyEnum MyValue {get;set;}
    [ProtoMember(12)]
    private int MyValueWire {
        get {return (int)MyValue;}
        set {MyValue = (MyEnum)value;}
    }
    

    The other alternative would be to add a flag that works like the above on your behalf; treating it as an int rather than an enum.

    0 讨论(0)
提交回复
热议问题