问题
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