Object to byte array serialization/Deserialization : Serialization « File Stream « C# / C Sharp






Object to byte array serialization/Deserialization

       


namespace Qxado.IO
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.InteropServices;
    using System.Security;

    /// <summary>
    /// ??????????
    /// </summary>
    public class SerializationHelper 
    {

        #region ISerializationHelper ???

        /// <summary>
        /// ?????????????????????
        /// </summary>
        public byte[] Serialize2Bytes(object data)
        {
            if (data == null)
            {
                return new byte[0];
            }
            else
            {
                MemoryStream streamMemory = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(streamMemory, data);
                return streamMemory.GetBuffer();
            }
        }

        /// <summary>
        /// ?????????????????
        /// </summary>
        public object DeserializeFromBytes(byte[] binData)
        {
            if (binData == null)
            {
                return null;
            }
            else
            {
                if (binData.Length == 0)
                {
                    return null;
                }
                else
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    MemoryStream ms = new MemoryStream(binData);
                    return formatter.Deserialize(ms);
                }
            }
        }

        /// <summary>
        /// ?????????????????
        /// </summary>
        public string Serialize2String(object data)
        {
            if (data == null)
            {
                return string.Empty;
            }
            else
            {
                MemoryStream streamMemory = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(streamMemory, data);
                return Convert.ToBase64String(streamMemory.GetBuffer());
            }
        }

        /// <summary>
        /// ?????????????
        /// </summary>
        public object DeserializeFromString(string binString)
        {
            if (binString == null)
            {
                return null;
            }
            else
            {
                if (binString.Length == 0)
                {
                    return null;
                }
                else
                {
                    byte[] binData = Convert.FromBase64String(binString);
                    BinaryFormatter formatter = new BinaryFormatter();
                    MemoryStream ms = new MemoryStream(binData);
                    return formatter.Deserialize(ms);
                }
            }
        }

        #endregion
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Use Serializable attribute to mark a generic class
2.Use Serializable attribute to mark a class
3.Deserialize Object
4.Serialize and DeSerialize
5.Three types of Serialization: Binary, Soap, XML
6.Use XML Serialization with Custom ObjectsUse XML Serialization with Custom Objects
7.Working with the Serializable Attribute
8.NonSerialized attributesNonSerialized attributes
9.Serialize hiearchy classesSerialize hiearchy classes
10.C# Serialization C# Serialization
11.Serial Employee class
12.Set XML tag name for Serialization
13.illustrates binary serializationillustrates binary serialization
14.Deserialize
15.Collection Serialization
16.Serializes an object to binary
17.Deserializes an object from binary
18.Object to string serialization/Deserialization
19.Javascript Serializer
20.Serialize and Deserialize (2)
21.Binary Serializer
22.Deserialize the incomming data to the T datatype, this method used to deserialize the test cases data to the required entity
23.Serialization Utility
24.Serialization Utilities
25.Clone With Serialization