You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 line
827 B

  1. using System.IO;
  2. using System.Runtime.Serialization.Formatters.Binary;
  3. namespace StuMgmLib
  4. {
  5. class Utility
  6. {
  7. /// <summary>
  8. /// 序列化
  9. /// </summary>
  10. public static byte[] SerializeBin<T>(T c)
  11. {
  12. MemoryStream ms = new MemoryStream();
  13. BinaryFormatter iFormatter = new BinaryFormatter();
  14. iFormatter.Serialize(ms, c);
  15. byte[] buf = ms.GetBuffer();
  16. return buf;
  17. }
  18. /// <summary>
  19. /// 反序列化
  20. /// </summary>
  21. public static T DeserializeBin<T>(byte[] buf)
  22. {
  23. MemoryStream ms = new MemoryStream(buf);
  24. BinaryFormatter iFormatter = new BinaryFormatter();
  25. var obj = (T)iFormatter.Deserialize(ms);
  26. return obj;
  27. }
  28. }
  29. }