Skip to content

Instantly share code, notes, and snippets.

@gscattolin
Created September 10, 2013 18:05
Show Gist options
  • Select an option

  • Save gscattolin/6513182 to your computer and use it in GitHub Desktop.

Select an option

Save gscattolin/6513182 to your computer and use it in GitHub Desktop.
byte array to get object
/// <summary>
/// Function to get object from byte array
/// </summary>
/// <param name="_ByteArray">byte array to get object</param>
/// <returns>object</returns>
public object ByteArrayToObject(byte[] _ByteArray)
{
try
{
// convert byte array to memory stream
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_ByteArray);
// create new BinaryFormatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// set memory stream position to starting point
_MemoryStream.Position = 0;
// Deserializes a stream into an object graph and return as a object.
return _BinaryFormatter.Deserialize(_MemoryStream);
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
// Error occured, return null
return null;
}
/// <summary>
/// Function to get byte array from a object
/// </summary>
/// <param name="_Object">object to get byte array</param>
/// <returns>Byte Array</returns>
public byte[] ObjectToByteArray(object _Object)
{
try
{
// create new memory stream
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
// create new BinaryFormatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// Serializes an object, or graph of connected objects, to the given stream.
_BinaryFormatter.Serialize(_MemoryStream, _Object);
// convert stream to byte array and return
return _MemoryStream.ToArray();
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
// Error occured, return null
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment