您的位置:首页 > 编程语言 > C#

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 代码如下:

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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: