Saturday, January 27, 2007

several stuff around formatters & serialization...

What is a formatter?

A formatter is what will serialize or deserialize.

- SerializableAttribute
- ISerializable

What is ISerializable?

The standard interface an object needs to implement for custom serialization.


The Serializable Attribute.

- [Serializable] ClassName

- Indicates that instances of a certain class can be serialized.

- Serializes both private and public members of a class.

- The quick and dirty way to serialization. (Not so dirty anymore).


Serialization (formatter helper) as code sample with the usage of generics:



namespace MergeSystem.Indexus.WinServiceCommon.Formatters
{

public static class Serialization
{
public static byte[] BinarySerialize(Object obj)
{
byte[] serializedObject;
MemoryStream ms = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(ms, obj);
ms.Seek(0, 0);
serializedObject = ms.ToArray();
ms.Close();
return serializedObject;
}

public static T BinaryDeSerialize(byte[] serializedObject)
{
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;
}
}
}

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge