[C#] C#自定义序列化 ISerializable 的实现 →→→→→进入此内容的聊天室

来自 , 2019-09-30, 写在 C#, 查看 144 次.
URL http://www.code666.cn/view/861637a4
  1.    [Serializable]
  2.     public class BaseObject
  3.     {
  4.         [OptionalField]
  5.         private string _str = "I am BaseObject";
  6.     }
  7.  
  8.     [Serializable]
  9.     public class TestObject : BaseObject, ISerializable
  10.     {
  11.         int a;
  12.  
  13.         string strName = "";
  14.  
  15.         Color c = Color.Red;
  16.  
  17.         DataTable _dtColors = null;
  18.         [OptionalField]
  19.         ArrayList list = new ArrayList();
  20.  
  21.         [OptionalField]
  22.         List<int> list1 = new List<int>();
  23.  
  24.         [OptionalField]
  25.         Dictionary<int, string> dic = new Dictionary<int, string>();
  26.         //当实现ISerializable接口时,如果该构造函数不存在,则会引发一个SerializationException异常
  27.  
  28.         //该特性表示,该方法只允许序列化器调
  29.  
  30.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  31.         protected TestObject(SerializationInfo info, StreamingContext context)
  32.         {
  33.             #region  如果基类也实现了ISerializable接口,则序列化器会自动调用基类的该构造函数,就不需要本段代码
  34.             Type basetype = this.GetType().BaseType;
  35.             MemberInfo[] mi = FormatterServices.GetSerializableMembers(basetype, context);
  36.  
  37.             for (int i = 0; i < mi.Length; i++)
  38.             {
  39.                 //由于AddValue不能添加重名值,为了避免子类变量名与基类变量名相同,将基类序列化的变量名加上基类类名
  40.                 FieldInfo fi = (FieldInfo)mi[0];
  41.                 object objValue = info.GetValue(basetype.FullName + "+" + fi.Name, fi.FieldType);
  42.                 fi.SetValue(this, objValue);
  43.             }
  44.  
  45.             #endregion
  46.             a = info.GetInt32("a");
  47.             strName = info.GetString("strName");
  48.             c = (Color)info.GetValue("c", typeof(Color));
  49.             _dtColors = (DataTable)info.GetValue("_dtColors", typeof(DataTable));
  50.             list = (ArrayList)info.GetValue("list", typeof(ArrayList));
  51.             list1 = (List<int>)info.GetValue("list1", typeof(List<int>));
  52.             dic = (Dictionary<int, string>)info.GetValue("dic", typeof(Dictionary<int, string>));
  53.         }
  54.  
  55.         public TestObject()
  56.         {
  57.             a = 100;
  58.             strName = "daps";
  59.             InitColorTable();
  60.             list1.Add(10);
  61.            list1.Add(20);      
  62.         }
  63.         #region ISerializable 成员
  64.  
  65.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =true)]
  66.         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  67.         {
  68.             info.AddValue("a", a);
  69.             info.AddValue("strName", strName);
  70.             info.AddValue("c", c);
  71.             info.AddValue("_dtColors", _dtColors);
  72.             info.AddValue("list", list);
  73.             info.AddValue("list1", list1);
  74.             info.AddValue("dic", dic);
  75.             Type basetype = this.GetType().BaseType;
  76.             MemberInfo[] mi = FormatterServices.GetSerializableMembers(basetype, context);
  77.  
  78.             for (int i = 0; i < mi.Length; i++)
  79.             {
  80.                 //由于AddValue不能添加重名值,为了避免子类变量名与基类变量名相同,将基类序列化的变量名加上基类类名
  81.                 info.AddValue(basetype.FullName + "+" + mi[i].Name, ((FieldInfo)mi[i]).GetValue(this));
  82.             }
  83.         }
  84.         #endregion
  85.     }
  86. //csharp/6351

回复 "C#自定义序列化 ISerializable 的实现"

这儿你可以回复上面这条便签

captcha