using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf.Serializers;
namespace Prorobuf_net_ProtoContract_
{
public class ProtoBuf_Serialize
{
static void Main()
{
ErrorLog errorLog = new ErrorLog();
errorLog.LogID = "1234";
Byte[] bytes = Serialize(errorLog);
string sa = Encoding.UTF8.GetString(bytes);
Console.WriteLine(sa);
}
/// <summary>
/// 将消息序列化为二进制的方法
/// </summary>
/// <param name="log">要序列化的对象</param>
/// <returns></returns>
public static byte[] Serialize(ErrorLog log)
{
// 涉及格式转换,需要用到流,将二进制序列化到流中
using (MemoryStream memoryStream = new MemoryStream())
{
//使用ProtoBuf工具的序列
ProtoBuf序列化和反序列化方法
最新推荐文章于 2025-09-06 16:09:49 发布
该代码示例展示了如何使用ProtoBuf-net库对ErrorLog对象进行二进制序列化和反序列化。通过MemoryStream处理数据流,利用ProtoBuf.Serializer进行序列化和反序列化操作,实现对象与字节数据间的转换。

最低0.47元/天 解锁文章

3933

被折叠的 条评论
为什么被折叠?



