Guest User

Untitled

a guest
Nov 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. public byte[] ToByteArray<T>(T obj)
  2. {
  3. if(obj == null)
  4. return null;
  5. BinaryFormatter bf = new BinaryFormatter();
  6. using(MemoryStream ms = new MemoryStream())
  7. {
  8. bf.Serialize(ms, obj);
  9. return ms.ToArray();
  10. }
  11. }
  12.  
  13. public T FromByteArray<T>(byte[] data)
  14. {
  15. if(data == null)
  16. return default(T);
  17. BinaryFormatter bf = new BinaryFormatter();
  18. using(MemoryStream ms = new MemoryStream(data))
  19. {
  20. object obj = bf.Deserialize(ms);
  21. return (T)obj;
  22. }
  23. }
Add Comment
Please, Sign In to add comment