/// <summary> ///To turn a serializable object into a byte array/// </summary> /// <param name= "obj" >object (object cannot be empty)</param> /// <returns>returns the related array</returns> protected Static byte[] objecttobytearray<t> (T obj)wheret:iserializable {if(obj = =NULL) { byte[] Bytearr =New byte[] { }; returnBytearr; } Else{MemoryStream ms=NewMemoryStream (); BinaryFormatter BF=NewBinaryFormatter (); Bf. Serialize (MS, obj); Ms. Close (); returnMs. ToArray (); } } /// <summary> ///reverts a serializable object to a byte array to an object/// </summary> /// <param name= "Bytearry" >byte array</param> /// <returns>related Objects</returns> protected StaticT bytearraytoobject<t> (byte[] bytearry)wheret:iserializable {if(Bytearry! =NULL&& bytearry.length >0) {MemoryStream ms=NewMemoryStream (Bytearry); BinaryFormatter BF=NewBinaryFormatter (); return(T) bf. Deserialize (MS); } Else { return default(T); } }
Reciprocal transfer between serializable objects and byte[] arrays