【问题标题】:What's the difference between "Int32" and "google.protobuf.Int32Value"?\"Int32\" 和 \"google.protobuf.Int32Value\" 有什么区别?
【发布时间】:2022-12-12 02:22:43
【问题描述】:

我正在遗留 C# 代码库上编写 proto3 消息,我注意到如果我替换

import "google/protobuf/wrappers.proto";
import "google/protobuf/descriptor.proto";

message Foo {
    google.protobuf.Int32Value blah = 1;
}

和:

message Foo {
    int32 blah = 1;
}

解析器仍然有效,但 blah 收到 0 而不是正确的值。

为什么会这样? int32 不应该等同于 google.protobuf.Int32Value 吗?

【问题讨论】:

    标签: c# protocol-buffers


    【解决方案1】:

    一般来说,C# 中的 Int32 类型和 Google 的 Protocol Buffers 中的 Int32Value 类型是不等价的。 Int32 是 C# 中的原始数据类型,表示 32 位有符号整数,而 Int32Value 是 Protocol Buffers 中的包装类,允许将 32 位有符号整数表示为消息。这意味着 Int32Value 可以用作 Protocol Buffers 消息中的字段,而 Int32 不能。

    也就是说,可能有一些方法可以将 Int32 值与 Protocol Buffers 一起使用,例如将它们转换为 Int32Value 对象,或者将它们用作可在 Protocol Buffers 消息中使用的其他类型的字段的值。也有可能某些 Protocol Buffers 实现或工具可能会为直接将 Int32 值与 Protocol Buffers 一起使用提供特定支持,但这将取决于具体实现,并且不会成为 Protocol Buffers 的一般特性。

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2011-12-12
      相关资源
      最近更新 更多