Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. public static 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 static T FromByteArray<T>(byte[] data)
  14. {
  15. if(data == null)
  16. return null;
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement