一、说明
该序列化以二进制方式进行序列化,不要求类型相同,仅为相似结构即可。但是不支持接口、抽象类、继承类等成员的序列化,目前仅支持基础类型、自定义实体类、结构体、数据、字典、List。
二、序列化
public byte[] RRQMBinarySerialize(object obj)
{
using (ByteBlock byteBlock = new ByteBlock())
{
RRQMBinarySerialize(byteBlock, obj);
return byteBlock.ToArray();
}
}
三、反序列化
public object RRQMBinaryDeserialize(byte[] data, int offset, Type type)
{
RRQMBinaryFormatter bf = new RRQMBinaryFormatter();
return bf.Deserialize(data, offset, type);
}
四、快捷序列化
在静态类SerializeConvert
中,提供了快捷的序列化方式,以供方便使用。
五、性能对比
待测试类
[Serializable]
public class Student
{
public int P1 { get; set; }
public string P2 { get; set; }
public long P3 { get; set; }
public byte P4 { get; set; }
public DateTime P5 { get; set; }
public double P6 { get; set; }
public byte[] P7 { get; set; }
public List<int> List1 { get; set; }
public List<string> List2 { get; set; }
public List<byte[]> List3 { get; set; }
public Dictionary<int, int> Dic1 { get; set; }
public Dictionary<int, string> Dic2 { get; set; }
public Dictionary<string, string> Dic3 { get; set; }
public Dictionary<int, Arg> Dic4 { get; set; }
}
[Serializable]
public class Arg
{
public Arg(int myProperty)
{
this.MyProperty = myProperty;
}
public Arg()
{
Person person = new Person();
person.Name = "张三";
person.Age = 18;
}
public int MyProperty { get; set; }
}
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
测试代码
Student student = new Student();
student.P1 = 10;
student.P2 = "若汝棋茗";
student.P3 = 100;
student.P4 = 0;
student.P5 = DateTime.Now;
student.P6 = 10;
student.P7 = new byte[1024 * 64];
Random random = new Random();
random.NextBytes(student.P7);
student.List1 = new List<int>();
student.List1.Add(1);
student.List1.Add(2);
student.List1.Add(3);
student.List2 = new List<string>();
student.List2.Add("1");
student.List2.Add("2");
student.List2.Add("3");
student.List3 = new List<byte[]>();
student.List3.Add(new byte[1024]);
student.List3.Add(new byte[1024]);
student.List3.Add(new byte[1024]);
student.Dic1 = new Dictionary<int, int>();
student.Dic1.Add(1, 1);
student.Dic1.Add(2, 2);
student.Dic1.Add(3, 3);
student.Dic2 = new Dictionary<int, string>();
student.Dic2.Add(1, "1");
student.Dic2.Add(2, "2");
student.Dic2.Add(3, "3");
student.Dic3 = new Dictionary<string, string>();
student.Dic3.Add("1", "1");
student.Dic3.Add("2", "2");
student.Dic3.Add("3", "3");
student.Dic4 = new Dictionary<int, Arg>();
student.Dic4.Add(1, new Arg(1));
student.Dic4.Add(2, new Arg(2));
student.Dic4.Add(3, new Arg(3));
BytePool bytePool = new BytePool(1024 * 1024 * 10, 102400);
TimeSpan timeSpan1 = TimeMeasurer.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
ByteBlock byteBlock = bytePool.GetByteBlock(1024 * 100);
SerializeConvert.RRQMBinarySerialize(byteBlock, student,true);
Student student1 = SerializeConvert.RRQMBinaryDeserialize<Student>(byteBlock.Buffer,
byteBlock.Dispose();
if (i % 1000 == 0)
{
Console.WriteLine(i);
}
}
});
TimeSpan timeSpan2 = TimeMeasurer.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
ByteBlock byteBlock = bytePool.GetByteBlock(1024 * 100);
SerializeConvert.BinarySerialize(byteBlock, student);
byteBlock.Position = 0;
Student student1 = SerializeConvert.BinaryDeserialize<Student>(byteBlock);
byteBlock.Dispose();
if (i % 1000 == 0)
{
Console.WriteLine(i);
}
}
});
Console.WriteLine($"RRQM:{timeSpan1}");
Console.WriteLine($"System:{timeSpan2}");
测试结果