簡體   English   中英

Google Protocol Buffers - 序列化為字節數組

[英]Google Protocol Buffers - serialize to byte array

我正在按照教程使用Google Protocol Buffers for C#。 我沒有看到將對象轉換為字節數組的示例 - 有誰知道如何做到這一點? 我使用protoc編譯器自動生成了一個FilePath類,到目前為止:

FilePath fp = new FilePath
{
    Path = "TestPath",
    RealTimeMultiple = 5.0f
};

所以,我需要知道如何在不使用BinaryFormatter的情況下正確序列化fp對象。

假設您使用的是Google.Protobuf nuget包,您可以使用:

using Google.Protobuf;

...

byte[] bytes = fp.ToByteArray();

您需要using Google.Protobufusing指令來使IMessage.ToByteArray擴展方法可用 - 這可能是您之前缺少的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM