简体   繁体   English

Visual Studio 从 gRPC.proto 文件自动生成代码

[英]Visual Studio auto generated code from gRPC .proto file

I'm facing an issue where it seems that Visual Studio is changing the way objects look in a.proto file.我面临一个问题,Visual Studio 似乎正在改变对象在 .proto 文件中的外观。

.proto file: concordium_p2p_rpc.proto .proto 文件: concordium_p2p_rpc.proto

Example - this is in the.proto file:示例 - 这是在 .proto 文件中:

message BlockHash {
  string block_hash = 1;
}

After building my project I want to create an instance of this exact object, however, the property inside the object is now called "BlockHash_" and not "block_hash" as in the.proto file.在构建我的项目后,我想创建一个确切的 object 的实例,但是,object 内部的属性现在称为“BlockHash_”,而不是像 .proto 文件中的“block_hash”。

The confusion is complete if I try to serialize this object to JSON... Then the attribute is now called "blochHash"... a 3rd name.如果我尝试将此 object 序列化为 JSON ... 那么该属性现在称为“blochHash”...第三个名称。

So in.proto所以在.proto

{
    "block_hash": ""
}

becomes in C#变成 C#

BlockHash bh = new BlockHash()
{
    BlockHash_: ""
}

and then when serialized然后在序列化时

{
    "blockHash": ""
}

What is with that?那是怎么回事?

OK, so I found the answer to this question myself in this article.好的,所以我在这篇文章中自己找到了这个问题的答案。

https://visualstudiomagazine.com/articles/2020/01/06/defining-grpc-messages.aspx https://visualstudiomagazine.com/articles/2020/01/06/defining-grpc-messages.aspx

Why Microsoft decides to perform this renaming instead of just keeping the names are they are in the.proto file, is beyond me and evidently it introduces problems that I don't see would have been there otherwise.为什么 Microsoft 决定执行此重命名而不是仅将名称保留在 .proto 文件中,这超出了我的理解范围,显然它引入了我看不到的问题,否则会出现问题。

What happens is that Visual Studio wants to be the boss of what things are called, and therefore it wants to rename the attribute "block_hash" of the object BlockHash by making the first letter upper case, removing the underscore and making the first letter of the next word an upper case as well => "BlockHash".发生的情况是,Visual Studio 想成为所谓的事情的老板,因此它想通过将第一个字母变为大写、删除下划线并将第一个字母变为下一个单词也是大写=>“BlockHash”。

However, since this is the name of the object/class itself, this name is probably problematic since class constructors use this name and therefore Visual Studio fixes it's own dodo be adding a tailing underscore, resulting in an attribute named "BlockHash_".但是,由于这是对象/类本身的名称,因此该名称可能有问题,因为 class 构造函数使用此名称,因此 Visual Studio 修复了它自己的渡渡鸟添加尾下划线,从而产生名为“BlockHash_”的属性。

Why oh why do you do these things?.....为什么哦,你为什么要做这些事情?......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM