ProtobufExtensions.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using ProtoBuf;
  6. namespace Grpc.Extension.Common
  7. {
  8. /// <summary>
  9. /// ProtobufExtensions
  10. /// </summary>
  11. public class ProtobufExtensions
  12. {
  13. /// <summary>
  14. /// 序列化
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="input"></param>
  18. /// <returns></returns>
  19. public static byte[] Serialize<T>(T input)
  20. {
  21. using (MemoryStream memoryStream = new MemoryStream())
  22. {
  23. Serializer.Serialize<T>((Stream)memoryStream, input);
  24. return memoryStream.ToArray();
  25. }
  26. }
  27. /// <summary>
  28. /// 反序列化
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. /// <param name="data"></param>
  32. /// <returns></returns>
  33. public static T Deserialize<T>(byte[] data)
  34. {
  35. using (MemoryStream memoryStream = new MemoryStream(data))
  36. return Serializer.Deserialize<T>((Stream)memoryStream);
  37. }
  38. }
  39. }