Google C# 的 Protobuf 3.0.0 程序集

Google Protobuf 3.0.0 assemblies for C#

在我们的项目中,我们成功地使用了 Google Protobuf for C++。现在需要编译相同的 *.proto 文件以在 C# 代码中使用它。我下载了最近的Protobuf version 3.0.0-alpha-3。它为 C# 提供了 proto2 格式的支持,这对我来说已经足够了。我可以成功构建我的 *.proto 文件并获得 *.cs 文件。但是,当我将生成的 *.cs 文件添加到我的 C# 项目并尝试构建时,我收到如下编译器错误:"The type or namespace name 'Google' could not be found in the global namespace (are you missing an assembly reference?)" 这是发生错误的地方:

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: DiagramExport.proto
#pragma warning disable 1591, 0612, 3021
#region Designer generated code

using pb = global::Google.ProtocolBuffers;
using pbc = global::Google.ProtocolBuffers.Collections;
using pbd = global::Google.ProtocolBuffers.Descriptors;

现在我在项目页面上的发布 ZIP 中找不到任何 DLL 等,我可以将其作为参考包含在我的 C# 项目中。只有 protoc.exe 和一些 *.proto 文件在那里。我的简单问题是:我从哪里得到这些程序集?

(备注:我尝试按照 README 文件中的说明从源代码构建项目 protobuf-csharp-3.0.0-alpha-3,但未能使用 Visual Studio 2013 Update 4 "out of the box" 构建它;我得到一些编译器错误。)

阅读 this and this 文档页面后,我发现可以通过在程序包管理器控制台中执行以下命令为我的项目安装 Protocol Buffers NuGet 程序包:

Install-Package Google.ProtocolBuffers

控制台可在 Visual Studio 2013 年通过工具 --> NuGet 包管理器 --> 包管理器控制台访问。经理下载了这个包,我在我的项目中得到了两个引用 "Google.ProtocolBuffers" 和 "Google.ProtocolBuffers.Serialization",这让编译器很满意。它现在完美运行!

查看发行说明 here

C#(测试版) 部分下,您会发现:

Breaking: Preconditions is renamed to ProtoPreconditions
Breaking: GeneratedCodeInfo is renamed to GeneratedClrTypeInfo

因此 Grpc.Tools 包附带的 protoc.exe 似乎生成了 "old" 代码。我用 this 替换了 protoc.exe 并重新编译(重新生成)我的 类 解决了问题。