我刚开始使用Google Protocol Buffers和Marc Gravell非常棒的protobuf-net程序,我不明白的一件事是生成的.proto文件中字段声明的命名约定。
以下是Google推荐的内容:
“对字段名称使用underscore_separated_names - 例如,song_name。” https://developers.google.com/protocol-buffers/docs/style
“请注意,方法名称始终使用驼峰式命名,即使.proto文件中的字段名称使用带有下划线的小写字母(应该如此)。” https://developers.google.com/protocol-buffers/docs/reference/java-generated
“注意这些访问器方法如何使用驼峰式命名,即使.proto文件使用带有小写的下划线。” https://developers.google.com/protocol-buffers/docs/javatutorial
但是当我在protobuf-net中使用Serializer.GetProto()方法时:
[ProtoContract]
public partial class AuthEntry
{
private string _windowsAccount = "";
private string _machineNames = "*";
[ProtoMember(1)]
public string WindowsAccount
{
get { return _windowsAccount; }
set { _windowsAccount = value; }
}
[ProtoMember(2)]
public string MachineNames
{
get { return _machineNames; }
set { _machineNames = value; }
}
}
我明白了:
message AuthEntry {
optional string WindowsAccount = 1;
optional string MachineNames = 2;
}
而不是像我期望的那样:
message AuthEntry {
optional string windows_account = 1;
optional string machine_names = 2;
}
我猜这没什么大不了的,但以防万一......
答案 0 :(得分:1)
原型生成并不会尝试应用这些约定,因为它会进入消除歧义,冲突等的军备竞赛 - 更不用说在CustomerIDReference这样的任意名称中找到单词分词的乐趣(好吧,这不太可能例如,但你明白了。)如果您想自己控制它 - 在ProtoContractAttribute或ProtoMemberAttribute上指定Name属性。