From Framework 1.1 I’m use in static class for serialize and deserialize objects. In Framework 2.0 C# got generics and class make away with word “object” but evolution is go on and time has come to create Serializer as extension:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Xml.Serialization;
static class Serializer
{
/// <summary>
/// Deserializes from binary.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="bytes">buffer</param>
/// <returns></returns>
public static T DeserializeFromBinary<T>(this byte[] bytes) where T : class
{
T obj = default(T);
MemoryStream memoryStream = new MemoryStream(bytes);
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
obj = (T)binaryFormatter.Deserialize(memoryStream);
}
catch (SerializationException exception)
{
throw;
}
return obj;
}
/// <summary>
/// Deserializes from file.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="path">Path.</param>
/// <returns></returns>
public static T DeserializeFromFile<T>(this string path) where T : class
{
T obj = default(T);
FileStream fileStream = new FileStream(path, FileMode.Open);
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
obj = (T)binaryFormatter.Deserialize(fileStream);
}
catch (SerializationException exception)
{
throw;
}
finally
{
fileStream.Close();
}
return obj;
}
/// <summary>
/// Deserializes from string that contains SOAP.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="soap">S.</param>
/// <returns></returns>
public static T DeserializeFromSoap<T>(this string soap) where T : class
{
T obj = default(T);
MemoryStream memoryStream = new MemoryStream(new UTF8Encoding().GetBytes(soap));
try
{
SoapFormatter soapFormatter = new SoapFormatter();
obj = (T)soapFormatter.Deserialize(memoryStream);
}
catch (SerializationException exception)
{
throw;
}
return obj;
}
/// <summary>
/// Deserializes from XML string
/// </summary>
/// <param name="type">Type.</param>
/// <param name="s">S.</param>
/// <returns></returns>
public static T DeserializeFromXml<T>(this string xml) where T : class
{
T obj = default(T);
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
obj = (T)xmlSerializer.Deserialize(new StringReader(xml));
}
catch (SerializationException exception)
{
throw;
}
return obj;
}
/// <summary>
/// Deserializes from XML file.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="xmlFilePath">file path.</param>
/// <returns></returns>
public static T DeserializeFromXmlFile<T>(this string xmlFilePath) where T : class
{
T obj = default(T);
FileStream fileStream = default(FileStream);
try
{
fileStream = new FileStream(xmlFilePath, FileMode.Open);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
obj = (T)xmlSerializer.Deserialize(fileStream);
}
catch (Exception exception)
{
throw;
}
finally
{
fileStream.Close();
}
return obj;
}
/// <summary>
/// Gets the size of the object.
/// </summary>
/// <param name="obj">O.</param>
/// <returns></returns>
public static long GetByteSize<T>(this T obj) where T : class
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
binaryFormatter.Serialize(memoryStream, obj);
return memoryStream.Length;
}
/// <summary>
/// Serializes to binary stream.
/// </summary>
/// <param name="obj">Obj.</param>
/// <returns></returns>
public static byte[] SerializeToBinary<T>(this T obj) where T : class
{
byte[] buffer = new byte[0x9c4];
MemoryStream memoryStream = new MemoryStream();
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, obj);
memoryStream.Seek(0, SeekOrigin.Begin);
if (memoryStream.Length > buffer.Length)
{
buffer = new byte[memoryStream.Length];
}
buffer = memoryStream.ToArray();
}
catch (Exception exception)
{
throw;
}
finally
{
memoryStream.Close();
}
return buffer;
}
public static void SerializeToBinary<T>(this T obj, string filePath) where T : class
{
SerializeToBinary(obj, filePath, FileMode.Create);
}
public static void SerializeToBinary<T>(this T obj, string filePath, FileMode mode) where T : class
{
FileStream fileStream = default(FileStream);
BinaryFormatter binaryFormatter = new BinaryFormatter();
try
{
fileStream = new FileStream(filePath, mode);
binaryFormatter.Serialize(fileStream, obj);
}
catch (Exception exception)
{
throw;
}
finally
{
fileStream.Close();
}
}
public static string SerializeToSoap<T>(this T obj) where T : class
{
string returnValue = string.Empty;
MemoryStream memoryStream = new MemoryStream();
try
{
SoapFormatter soapFormatter = new SoapFormatter();
soapFormatter.Serialize(memoryStream, obj);
memoryStream.Seek(0, SeekOrigin.Begin);
returnValue = Encoding.ASCII.GetString(memoryStream.ToArray());
}
catch (Exception exception)
{
throw;
}
finally
{
memoryStream.Close();
}
return returnValue;
}
public static void SerializeToSoap<T>(this T obj, string filePath, FileMode mode) where T : class
{
FileStream fileStream = default(FileStream);
SoapFormatter soapFormatter = new SoapFormatter();
try
{
fileStream = new FileStream(filePath, mode);
soapFormatter.Serialize(fileStream, obj);
}
catch (SerializationException exception)
{
throw;
}
finally
{
fileStream.Close();
}
}
public static string SerializeToXml<T>(this T obj) where T : class
{
string returnValue = string.Empty;
MemoryStream memoryStream = new MemoryStream();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(memoryStream, obj);
memoryStream.Seek(0, SeekOrigin.Begin);
returnValue = Encoding.ASCII.GetString(memoryStream.ToArray());
}
catch (SerializationException exception)
{
throw;
}
finally
{
memoryStream.Close();
}
return returnValue;
}
public static void SerializeToXmlFile<T>(this T obj, string filePath, FileMode mode) where T : class
{
FileStream fileStream = default(FileStream);
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
fileStream = new FileStream(filePath, mode);
serializer.Serialize(fileStream, obj);
}
catch (Exception exception)
{
throw;
}
finally
{
fileStream.Close();
}
}
}
A method limited to using by class only but not prevent from errors. Examples: