Converts (Deserializes) a byte array to an object. - CSharp File IO

CSharp examples for File IO:Serialization

Description

Converts (Deserializes) a byte array to an object.

Demo Code


using System.Runtime.Serialization.Formatters.Binary;
using System.IO;//from   w w  w . j a  v a  2 s  .  com
using System;

public class Main{
        /// <summary>
        /// Converts (Deserializes) a byte array to an object.
        /// </summary>
        /// <typeparam name="T">Type of the object.</typeparam>
        /// <param name="arrBytes">Byte array.</param>
        /// <returns>Object obtained.</returns>
        public static T DeserializeFromByteArray<T>(byte[] arrBytes)
        {
            MemoryStream memStream = new MemoryStream();
            BinaryFormatter binForm = new BinaryFormatter();
            memStream.Write(arrBytes, 0, arrBytes.Length);
            memStream.Seek(0, SeekOrigin.Begin);
            T obj = (T)binForm.Deserialize(memStream);
            return obj;
        }
}

Related Tutorials