需要澄清Protobuf-net RuntimeTypeModel

时间:2014-05-23 12:36:54

标签: protobuf-net

我正在使用带有DataContracts的Protobuf-net。我这样注册类型:

RuntimeTypeModel.Default[typeA].AddSubType(fieldNumber, typeB);

问题是fieldNumber在protobuf-net代码中被明确限制为接受> 0< Int32.Max/4的整数。

internal static void WriteHeaderCore(int fieldNumber, WireType wireType, ProtoWriter writer)
        {
            uint header = (((uint)fieldNumber) << 3)
                | (((uint)wireType) & 7);
            WriteUInt32Variant(header, writer);
        }

Line 185

这样做的原因是什么?向后兼容性? CrossPlatform兼容性?可能的错误?

更新以显示我如何使用DataContracts

    // this guid is transformed to integer and it is a special one so brotobuf-net does not blow up.
    // That way I can safely rename the class whenever I want. Same for properies.
    [DataContract(Name = "c8978654-4380-44d2-8ebe-ae17a463dfb6")] 
    public class UserState 
    {
        UserState() { }

        [DataMember(Order = 1)]
        public override UserId Id { get; set; }

        [DataMember(Order = 2)]
        public string Firstname { get; private set; }

    }

1 个答案:

答案 0 :(得分:2)

符合protobuf spec

  

您可以指定的最小标记号是1,最大的是2 ^ 29 - 1或536,870,911。您也不能使用数字19000至19999