compile protocol buffers 3 timestamp type in c# visual studio?

眉间皱痕 提交于 2021-01-27 09:55:06

问题


Visual Studio 2015 
C#
NuGet Packages : 
Google.Protobuf v3.0.0 
Google.Protobuf.Tools v3.0.0

MessageType Quake

syntax = "proto3";
import "google/protobuf/timestamp.proto";
message Quake {
  google.protobuf.Timestamp _timestamp = 1;
  double magnitude = 2;
}

timestamp.proto included in same folder

protoc 3.0.2 command-line compilation succeeds

But VS right-click proto file and select "Run Custom Tool" fails with error "The custom tool 'ProtoBufTool' failed."

to generate the C# classes from within Visual Studio ?


回答1:


The Protobuf documentation seems short (weak) when it explains how to execute protoc.exe. Here is how I do it in a small test project I am writing. I have installed TWO NuGet packages for my Protobuf project:

Google.Protobuf (C# runtime library for Protocol Buffers)
Google.Protobuf.Tools (protoc.exe)

In Visual Studio I have defined a Pre-build event command line. This is defined in the properties menu for the project:

..\..\..\packages\Google.Protobuf.Tools.3.6.0\tools\windows_x64\protoc -I="$(ProjectDir)."
    --csharp_out=$(ProjectDir)Model $(ProjectDir)MyOwn.proto

The resulting MyOwn.cs file lands in subdirectory "Model". It must be manually included in the project, to be automatically compiled. This is how I do it today. I hope this helps someone.




回答2:


Import the Timestamp and use the full namespace:

syntax = "proto3";
import "google/protobuf/timestamp.proto";

message Quake {
  google.protobuf.Timestamp _timestamp = 1;
  double magnitude = 2;
}

To generate the C# code, see their manual: https://developers.google.com/protocol-buffers/docs/csharptutorial#compiling-your-protocol-buffers



来源:https://stackoverflow.com/questions/39604227/compile-protocol-buffers-3-timestamp-type-in-c-sharp-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!