Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. //dictionary to byte[]
  2.  public byte[] ObjectToByteArray<T>(this T obj)
  3.     {
  4.         MemoryStream m = new MemoryStream();
  5.         if (obj != null)
  6.         {
  7.             BinaryFormatter b = new BinaryFormatter();
  8.             b.Serialize(m, obj);
  9.         }
  10.         return m.ToArray();
  11.     }
  12.  
  13. //byte[] to dictionary
  14. //T == the type (object) to return
  15. public T ByteArrayToObject<T>(this byte[] arrBytes)
  16.      {
  17.         if (arrBytes == null || arrBytes.Length < 1)
  18.             return default(T);
  19.  
  20.         BinaryFormatter binForm = new BinaryFormatter();
  21.  
  22.         //Whatever object you are trying to Deserialize it too
  23.         T obj = (T)binForm.Deserialize(new MemoryStream(arrBytes));
  24.  
  25.         return obj;
  26.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement