Convert a byte array to an Object.
//-----------------------------------------------------------------------
// <copyright file="Utility.cs" company="ParanoidMike">
// Copyright (c) ParanoidMike. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ParanoidMike
{
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Win32;
/// <summary>
/// Reusable functions for many uses.
/// </summary>
public static class Utility
{
/// <summary>
/// Convert a byte array to an Object.
/// Copied from http://snippets.dzone.com/posts/show/3897
/// </summary>
/// <param name="arrBytes">
/// The byte[] array to be converted.
/// </param>
/// <returns>
/// The object to which the byte array is converted.
/// </returns>
public static object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
object obj = (object)binForm.Deserialize(memStream);
return obj;
}
}
}
Related examples in the same category