Estoy haciendo unas pruebas con un servicio de cache propio en el que guardamos los datos en byte[] en unos diccionarios, así que a quien pueda interesar, aquí os dejo los métodos de serializacion y desserializacion:

public static byte[] ToByteArray(Object obj)
{
    if (obj == null) return null;

    MemoryStream ms = new MemoryStream();
    BinaryFormatter b = new BinaryFormatter();
    b.Serialize(ms, obj);
    byte[] data = ms.ToArray();
    ms.Close();
    return data;
}

public static T ToObjectSerialize<T>(byte[] serializedObject)
{
    if (serializedObject == null) return default(T);

    MemoryStream ms = new MemoryStream();
    ms.Write(serializedObject, 0, serializedObject.Length);
    ms.Seek(0, 0);
    BinaryFormatter b = new BinaryFormatter();
    Object obj = b.Deserialize(ms);
    ms.Close();
    return (T)obj;
}

Categories: , ,

One Response so far.

  1. [...] This post was mentioned on Twitter by Emilio Torrens. Emilio Torrens said: Post en the .NET way: Objetos 2 byte[] http://cli.gs/X9EED [...]

Leave a Reply


*

PUBLICIDAD

EMAIL





posts recientes

MapReduce con MongoDB

Posted on may - 18 - 2012

0 Comment

Habilitar la compresión de...

Posted on abr - 25 - 2012

0 Comment

Bamboo, MSBuild y referencias...

Posted on abr - 17 - 2012

2 Comments

Sponsors

  • Etooltech
  • Dingus Services
  • Etooltech
  • Dingus Services