C# 使用 protobuf 进行对象序列化与反序列化
2015-12-17 08:55
956 查看
本文永久地址: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 代码如下:
测试用例代码如下:
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 = new System.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, int length) |
36 | { |
37 | if (bytesData.Length == 0) |
38 | { |
39 | return default (T); |
40 | } |
41 | try |
42 | { |
43 | System.IO.MemoryStream memoryStream = new System.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 | } |
相关文章推荐
- c# KeyDown,KeyPress 和KeyUp
- C#方法的练习
- C# 图片旋转和获取原图片旋转角度
- Labwindows/CVI程序调用C#生成的Dll文件
- C# 动态生成RDLC报表文件
- 【C#/OpenGL】基于DEM数据的3D地形绘制(基于CSGL)
- c# 基础知识——属性、字段
- C#_抽象类和抽象方法
- pp看书笔记---C#高级编程第九版 第六章 【数组】
- C#_密封类和密封方法
- 在 C# 中实现 Singleton (来源MSDN技术文章)
- c# 委托
- Singleton模式的实现方式 C#
- C#使用私钥进行RSA加密
- 面向对象
- C#控制属性
- C#中的static、readonly与const的比较
- C#的NHibernate的CRUD开发入门代码例子
- C# 字符串转换成日期,两个日期值比较大小,求两个日期相差天数
- 【鸟瞰】C#的学习