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

时间:2017-02-15 20:51:57

标签: c# file serialization protocol-buffers memorystream

我正在使用带有C#的Google Protocol Buffers,我无法为我拥有的对象设置字段。我有两个.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对象。

1 个答案:

答案 0 :(得分:0)

想出来。您所要做的就是设置字段并将其放在括号中。所以在我的例子中,我需要这样做:

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