Serialize and Deserialize

  • 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.
  • Binary serialize (BinaryFormatter) doesn't understand [DataContract].
  • 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;
         }
      }
   }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值