在一次项目开发中遇到需要WebService中需要返回Hashtable的情况。当经过一次又一次的尝试以后发现,WebService是不能使用Hashtable的,然后我们才去的办法是把数据转换成一个String或者String数组然后进行传递。但是这样的转换很消耗资源。于是我们想到了序列化。
方式一(必须把类设置为可序列化,序列化后较大但是较方便)
/// <summary>
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public byte[] SerializeObject(object pObj)
{
if (pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory, pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read, 0, read.Length);
_memory.Close();
return read;
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if (pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
方式二(可不用设置类为可序列化,序列化后大小较小但较为麻烦)
类TestClasscs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace XLH
{
public class TestClasscs
{
private string name = "";
public string Name
{
get { return name; }
set { name = value; }
}
private int _age = 1;
public int Age
{
get { return _age; }
set { _age = value; }
}
private bool _sex = false;
public bool Sex
{
get { return _sex; }
set { _sex = value; }
}
private Test1 _test = new Test1();
public Test1 Test
{
get { return _test; }
set { _test = value; }
}
/// <summary>
/// 序列化
/// </summary>
/// <returns></returns>
public byte[] GetBytes()
{
MemoryStream output = new MemoryStream();
BinaryWriter writer = new BinaryWriter(output);
writer.Write(this.Name);
writer.Write(this.Age);
writer.Write(this.Sex);
byte[] buff = this.Test.ToBytes();
writer.Write(buff.Length);
writer.Write(buff);
writer.Close();
output.Close();
return output.ToArray();
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="infos"></param>
/// <returns></returns>
public TestClasscs ToObject(byte[] infos)
{
if ((infos == null) || (infos.Length <= 4))
{
return null;
}
TestClasscs info = new TestClasscs();
MemoryStream input = new MemoryStream(infos);
BinaryReader reader = new BinaryReader(input);
info.Name = reader.ReadString();
info.Age = reader.ReadInt32();
info.Sex = reader.ReadBoolean();
int count = reader.ReadInt32();
info.Test = info.Test.ToObject(reader.ReadBytes(count));
reader.Close();
input.Close();
return info;
}
}
}
类Test1
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace XLH
{
public class Test1
{
private string s = "abcd";
public string S
{
get { return s; }
set { s = value; }
}
public byte[] ToBytes()
{
MemoryStream output = new MemoryStream();
BinaryWriter writer = new BinaryWriter(output);
writer.Write(this.s);
writer.Close();
output.Close();
return output.ToArray();
}
public Test1 ToObject(byte[] data)
{
if ((data == null) || (data.Length <= 4))
{
return null;
}
Test1 info = new Test1();
MemoryStream input = new MemoryStream(data);
BinaryReader reader = new BinaryReader(input);
info.S = reader.ReadString();
reader.Close();
input.Close();
return info;
}
}
}
浙公网安备 33010602011771号