一、说明

该序列化以二进制方式进行序列化,不要求类型相同,仅为相似结构即可。但是不支持接口、抽象类、继承类等成员的序列化,目前仅支持基础类型自定义实体类结构体数据字典List

二、序列化

  1. public byte[] RRQMBinarySerialize(object obj)
  2. {
  3. using (ByteBlock byteBlock = new ByteBlock())
  4. {
  5. RRQMBinarySerialize(byteBlock, obj);
  6. return byteBlock.ToArray();
  7. }
  8. }

三、反序列化

  1. public object RRQMBinaryDeserialize(byte[] data, int offset, Type type)
  2. {
  3. RRQMBinaryFormatter bf = new RRQMBinaryFormatter();
  4. return bf.Deserialize(data, offset, type);
  5. }

四、快捷序列化

在静态类SerializeConvert中,提供了快捷的序列化方式,以供方便使用。

image.png五、性能对比

待测试类

  1. [Serializable]
  2. public class Student
  3. {
  4. public int P1 { get; set; }
  5. public string P2 { get; set; }
  6. public long P3 { get; set; }
  7. public byte P4 { get; set; }
  8. public DateTime P5 { get; set; }
  9. public double P6 { get; set; }
  10. public byte[] P7 { get; set; }
  11. public List<int> List1 { get; set; }
  12. public List<string> List2 { get; set; }
  13. public List<byte[]> List3 { get; set; }
  14. public Dictionary<int, int> Dic1 { get; set; }
  15. public Dictionary<int, string> Dic2 { get; set; }
  16. public Dictionary<string, string> Dic3 { get; set; }
  17. public Dictionary<int, Arg> Dic4 { get; set; }
  18. }
  19. [Serializable]
  20. public class Arg
  21. {
  22. public Arg(int myProperty)
  23. {
  24. this.MyProperty = myProperty;
  25. }
  26. public Arg()
  27. {
  28. Person person = new Person();
  29. person.Name = "张三";
  30. person.Age = 18;
  31. }
  32. public int MyProperty { get; set; }
  33. }
  34. [Serializable]
  35. public class Person
  36. {
  37. public string Name { get; set; }
  38. public int Age { get; set; }
  39. }

测试代码

  1. Student student = new Student();
  2. student.P1 = 10;
  3. student.P2 = "若汝棋茗";
  4. student.P3 = 100;
  5. student.P4 = 0;
  6. student.P5 = DateTime.Now;
  7. student.P6 = 10;
  8. student.P7 = new byte[1024 * 64];
  9. Random random = new Random();
  10. random.NextBytes(student.P7);
  11. student.List1 = new List<int>();
  12. student.List1.Add(1);
  13. student.List1.Add(2);
  14. student.List1.Add(3);
  15. student.List2 = new List<string>();
  16. student.List2.Add("1");
  17. student.List2.Add("2");
  18. student.List2.Add("3");
  19. student.List3 = new List<byte[]>();
  20. student.List3.Add(new byte[1024]);
  21. student.List3.Add(new byte[1024]);
  22. student.List3.Add(new byte[1024]);
  23. student.Dic1 = new Dictionary<int, int>();
  24. student.Dic1.Add(1, 1);
  25. student.Dic1.Add(2, 2);
  26. student.Dic1.Add(3, 3);
  27. student.Dic2 = new Dictionary<int, string>();
  28. student.Dic2.Add(1, "1");
  29. student.Dic2.Add(2, "2");
  30. student.Dic2.Add(3, "3");
  31. student.Dic3 = new Dictionary<string, string>();
  32. student.Dic3.Add("1", "1");
  33. student.Dic3.Add("2", "2");
  34. student.Dic3.Add("3", "3");
  35. student.Dic4 = new Dictionary<int, Arg>();
  36. student.Dic4.Add(1, new Arg(1));
  37. student.Dic4.Add(2, new Arg(2));
  38. student.Dic4.Add(3, new Arg(3));
  39. BytePool bytePool = new BytePool(1024 * 1024 * 10, 102400);
  40. TimeSpan timeSpan1 = TimeMeasurer.Run(() =>
  41. {
  42. for (int i = 0; i < 100000; i++)
  43. {
  44. ByteBlock byteBlock = bytePool.GetByteBlock(1024 * 100);
  45. SerializeConvert.RRQMBinarySerialize(byteBlock, student,true);
  46. Student student1 = SerializeConvert.RRQMBinaryDeserialize<Student>(byteBlock.Buffer,
  47. byteBlock.Dispose();
  48. if (i % 1000 == 0)
  49. {
  50. Console.WriteLine(i);
  51. }
  52. }
  53. });
  54. TimeSpan timeSpan2 = TimeMeasurer.Run(() =>
  55. {
  56. for (int i = 0; i < 100000; i++)
  57. {
  58. ByteBlock byteBlock = bytePool.GetByteBlock(1024 * 100);
  59. SerializeConvert.BinarySerialize(byteBlock, student);
  60. byteBlock.Position = 0;
  61. Student student1 = SerializeConvert.BinaryDeserialize<Student>(byteBlock);
  62. byteBlock.Dispose();
  63. if (i % 1000 == 0)
  64. {
  65. Console.WriteLine(i);
  66. }
  67. }
  68. });
  69. Console.WriteLine($"RRQM:{timeSpan1}");
  70. Console.WriteLine($"System:{timeSpan2}");

测试结果

1.7 高性能序列化(TouchSocketBinaryFormatter) - 图3