If a class only has variables that already support serialize/deserialize such as string, int, and etc, we just need add the attribute [Serializabe] to the class.
If a class has static variables, it need implement the interface ISerializable. (Implement a customized constructor for deserialize and GetObjectData for serialize)
If a class A has a variable with your own defined class type B, while B has only primate types that already support serialize/deserialize such as string, int, and etc, then we just need add the attribute [Serializabe] to the class A and B.
[Serializable]
public class B
{
public string City { get; set; }
}
[Serializable]
public class A
{
public B BObject { get; set; }
}
If a class has some variables that don't support serialize/deserialize such as your own defined types that use other contract such as DataContract, it need implement the interface ISerializable.
If just Using the attribute [Serializable] to a class, it can serialize the dictionary variable; if the class implemented ISerializable interface, typically it cannot serialize the dictionary variable, and it need some extra work to serialize/deserialize dictionary.
The following sample is a typical use case: you can use binary serializer to nest datacontract serializer if needed.
[DataContract]
public class A
{
[DataMember]
public int Value1 {get; set;}
[DataMember]
public int Value2 {get; set;}
}
[ISerializable]
public class B : ISerializable // You can control serialize/deserialize for B
{
private A m_a;
// Called when deserialize
public B(SerializationInfo info, StreamingContext context)
{
string strA = (string)info.GetValue("A", typeof(string));
m_a = // using DataContractSerializer to deserialize (ReadObject) strA to m_a.
}
// Called when serialize, you can just serialize what you want in class A, for example, you just need serialize Value1 in class A
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
string strA = // using DataContractSerializer to serialize (WriteObject) m_a to strA.
info.AddValue("A", strA);
}
}
[ISerializable] // use this attribute is enough, no need to implement ISerializable
public class C
{
private IList<B> m_bs;
}
public class MySerializer
{
public MySerializer()
{
}
public static void Serialize(string fileName, C c)
{
try
{
using (Stream stream = File.Open(fileName, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, c);
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
public static C DeSerialize(string fileName)
{
try
{
using (Stream stream = File.Open(fileName, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
C c= (C)formatter.Deserialize(stream);
return c;
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
}
If a class only has variables that already support serialize/deserialize such as string, int, and etc, we just need add the attribute [Serializabe] to the class. If a class has static variables, it ne