繁体   English   中英

Google协议缓冲区-设置字段。

[英]Google Protocol Buffers - setting a field.

我正在使用带有C#的Google协议缓冲区,但无法为我拥有的对象设置字段。 我有两个.proto文件:filepath.proto和filepaths.proto。 Filepaths.proto包含Filepath.proto消息的重复字段。 这两个文件:

// filepath.proto

syntax = "proto3";
package TEST;

message FilePath
{
   string path = 1;
}

// filepaths.proto
syntax = "proto3";
import "filepath.proto";
package TEST;

message FilePaths
{
   repeated FilePath file_path = 1;
}

我知道如何创建FilePath对象:

如您所见,FilePaths消息使用多个FilePath对象。 我知道如何使用以下方法创建几个FilePath对象:

Google.Protobuf.Collections.RepeatedField<FilePath> filepaths = new Google.Protobuf.Collections.RepeatedField<FilePath>();
filepaths.Add(fp1);
filepaths.Add(fp2);
filepaths.Add(fp3);
filepaths.Add(fp4);
filepaths.Add(fp5);

其中fp1,fp2,fp3,fp4和fp5是我先前在代码中创建的FilePath对象。 我这样创建FilePaths对象:

FilePaths fpTest = new FilePaths
{
    Path = filepaths // ERROR HERE
};

Visual Studio告诉我FilePaths的“ Paths”字段是只读的,即我只得到一个GET而不是SET。 是否有解决此问题的方法? 基本上,我试图创建一个包含多个FilePath对象的FilePaths对象。

弄清楚了。 您所要做的就是设置字段并将其放在括号中。 因此,在我的示例中,我需要这样做:

FilePaths fpTest = new FilePaths
{
    Path = { filepaths }
};

暂无
暂无

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

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