C# 使用 protobuf 进行对象序列化与反序列化
来源:互联网 发布:5年php程序员工资 编辑:程序博客网 时间:2024/05/21 10:52
本文永久地址: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 =
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
}
0 0
- C# 使用 protobuf 进行对象序列化与反序列化
- C#使用Protocol Buffer(ProtoBuf)进行对象的序列化与反序列化
- 使用protoBuf进行序列化和反序列化
- 使用SerializableAttribute进行对象序列化与反序列化
- c#序列化与反序列化通用方法, 使用protobuf-net实现
- c#序列化与反序列化数据加密, 使用protobuf-net实现
- 【protobuf】 3.使用protobuf-csharp-port进行序列化和反序列化
- C#对象反序列化与对象序列化
- C#中如何进行序列化与反序列化
- C# 二进制BinaryFormatter进行序列化与反序列化
- C# Xml进行序列化与反序列化
- C# 二进制BinaryFormatter进行序列化与反序列化
- 使用C#语言进行序列化和反序列化
- spring-data-redis 使用 protobuf进行序列化和反序列
- C#对象的 Xml序列化与反序列化
- C#中对象的序列化与反序列化
- C#中对象的序列化与反序列化
- C#中对象的序列化与反序列化
- php生成两端留白缩略图类
- 面试题22:字符串包含(艺术)
- 产品设计切记空想意淫
- MyBatis传入参数与parameterType
- matlab的几个取整函数
- C# 使用 protobuf 进行对象序列化与反序列化
- 图片轮播
- nagios(二)配置
- oracle 11g RAC 常用命令整理分享
- java 跨域访问问题解决
- C#控件及常用设计整理
- 杭电ACM2018
- iOS获取屏幕宽高、设备型号、系统版本信息
- ArcGIS空间校正