Example for serialization

// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
    if(obj == null) return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);
    return ms.ToArray();
}
 
// Convert a byte array to an Object
private 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;
}

Download this snippet

Simple example for converting objects into byte arrays aka serialization. For custom classes add [Serializable] attribute to enable serialization.

Twitter Twitter

2 Comments to “ Example for serialization”

  1. Anonymous  on Mar 15, 2010

    lppi7w <a href="http://hdzncitskzfm.com/">hdzncitskzfm</a>, [url=http://hybwaymlzzhw.com/]hybwaymlzzhw[/url], [link=http://jqjgtbdhztqs.com/]jqjgtbdhztqs[/link], http://hpztzlldlpzf.com/

  2. Anonymous  on Jun 10, 2010

    wIrPTA <a href="http://stpoijifsinu.com/">stpoijifsinu</a>, [url=http://meqlohxcythr.com/]meqlohxcythr[/url], [link=http://ojtrpppmuufi.com/]ojtrpppmuufi[/link], http://xdaxpeyovvpv.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>