C# 使用 protobuf 进行对象序列化与反序列化

本文永久地址:http://www.omuying.com/article/148.aspx, 【文章转载请注明出处!】

protobuf 是 google的一个开源项目,可用于以下两种用途:(1)数据的存储(序列化和反序列化),类似于xml、json等;(2)制作网络通信协议。源代码下载地址:https://github.com/mgravell/protobuf-net;开源项目地址如下:https://code.google.com/p/protobuf-net/。

protobuf 工具类 DataUtils.cs 代码如下:
01 using ProtoBuf;
02 using System;
03 using System.Collections.Generic;
04  
05 /// <summary>
06 /// DataUtils 的摘要说明
07 /// </summary>
08 public class DataUtils
09 {
10     public static byte[] ObjectToBytes<T>(T instance)
11     {
12         try
13         {
14             byte[] array;
15             if (instance == null)
16             {
17                 array = new byte[0];
18             }
19             else
20             {
21                 System.IO.MemoryStream memoryStream = newSystem.IO.MemoryStream();
22                 Serializer.Serialize<T>(memoryStream, instance);
23                 array = new byte[memoryStream.Length];
24                 memoryStream.Position = 0L;
25                 memoryStream.Read(array, 0, array.Length);
26                 memoryStream.Dispose();
27             }
28             return array;
29         }
30         catch (System.Exception)
31         {
32             return new byte[0];
33         }
34     }
35     public static T BytesToObject<T>(byte[] bytesData, int offset, intlength)
36     {
37         if (bytesData.Length == 0)
38         {
39             return default(T);
40         }
41         try
42         {
43             System.IO.MemoryStream memoryStream = newSystem.IO.MemoryStream();
44             memoryStream.Write(bytesData, 0, bytesData.Length);
45             memoryStream.Position = 0L;
46             T result = Serializer.Deserialize<T>(memoryStream);
47             memoryStream.Dispose();
48             return result;
49         }
50         catch (System.Exception ex)
51         {
52             return default(T);
53         }
54     }
55 }
测试用例代码如下:
01 [ProtoContract]
02 class UserInfo
03 {
04     [ProtoMember(1)]
05     public int UserID;
06      
07     [ProtoMember(2)]
08     public string UserName;
09  
10     [ProtoMember(3)]
11     public List<ItemInfo> ItemList;
12  
13     // 默认构造函数必须有,否则反序列化会报 No parameterless constructor found for x 错误!
14     public UserInfo() { }
15  
16     public UserInfo(int userID, string userName)
17     {
18         this.UserID = userID;
19         this.UserName = userName;
20         this.ItemList = new List<ItemInfo>();
21     }
22 }
23  
24 [ProtoContract]
25 class ItemInfo
26 {
27     [ProtoMember(1)]
28     public int ItemID;
29  
30     [ProtoMember(2)]
31     public string ItemName;
32  
33     // 默认构造函数必须有,否则反序列化会报 No parameterless constructor found for x 错误!
34     public ItemInfo() { }
35  
36     public ItemInfo(int itemID, string itemName)
37     {
38         this.ItemID = itemID;
39         this.ItemName = itemName;
40     }
41 }
42  
43 using ProtoBuf;
44 using System;
45 using System.Collections.Generic;
46 using System.Linq;
47 using System.Web;
48 using System.Web.UI;
49 using System.Web.UI.WebControls;
50  
51 public partial class _Default : System.Web.UI.Page
52 {
53     protected void Page_Load(object sender, EventArgs e)
54     {
55         if (!IsPostBack)
56         {
57             UserInfo userInfo = new UserInfo(1, "user 1");
58             userInfo.ItemList.Add(new ItemInfo(1, "item 1"));
59             userInfo.ItemList.Add(new ItemInfo(2, "item 2"));
60  
61             byte[] userBytes = DataUtils.ObjectToBytes<UserInfo>(userInfo);
62  
63             UserInfo serUserInfo = DataUtils.BytesToObject<UserInfo>(userBytes, 0, userBytes.Length);
64  
65         }
66     }
67 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值