Google Protocol Buffers - 序列化为字节数组

Rok*_*545 5 c# serialization memorystream protocol-buffers

我正在按照教程使用Google Protocol Buffers for C#.我没有看到将对象转换为字节数组的示例 - 有谁知道如何做到这一点?我使用protoc编译器自动生成了一个FilePath类,到目前为止:

FilePath fp = new FilePath
{
    Path = "TestPath",
    RealTimeMultiple = 5.0f
};
Run Code Online (Sandbox Code Playgroud)

所以,我需要知道如何在不使用BinaryFormatter的情况下正确序列化fp对象.

Jon*_*eet 17

假设您正在使用Google.Protobufnuget包,您可以使用:

using Google.Protobuf;

...

byte[] bytes = fp.ToByteArray();
Run Code Online (Sandbox Code Playgroud)

您需要使用该using指令Google.Protobuf来使IMessage.ToByteArray扩展方法可用 - 这可能是您之前缺少的.