Serialization technology(序列化技术)

本文介绍了序列化技术,包括其主要作用,如持久化对象、用于.Net Remoting等。阐述了序列化属性,如标记类、跳过指定成员等。还说明了对象图管理、序列化过程,给出了序列化和反序列化类与对象的示例,最后提及了序列化涉及的安全问题及加密方法。

Serialization technology(序列化技术)

一、为什么要用序列化技术
主要作用:
1.Persistence(持久化对象):序列化到一个文件系统中
Store and retrieve a graph of objects to and from a file
2..Net Remoting
Pass by value arguments that are transmitted between processes
二、Serialization Attributes
1.To Mark a Class, Use Serializable Attribute
[Serializable] public class MyClass {}
2.To Skip Specified Members, Use NonSerialized Attribute
[Serializable] public class MyClass {   [NonSerialized] int _cashSize; //... }
3.To Provide Custom Serialization, Implement ISerializable
三、Object Graph
Serialization Manage Automatically
四、Serialization Process(序列化过程)
Classes Used by the Default Serialization Process
  ObjectIDGenerator – generates IDs for objects
  ObjectManager – tracks objects as they are being deserialized
Examples of Classes Used with Serialized Streams
  FileStream, MemoryStream, NetworkStream
Formatter Class
  Writes or reads data in a specified format to the output or input streams
  Runtime provides BinaryFormatter , SoapFormatter and XmlSerializer
五、Serialize Class and object
Example:
class SerializeExample
 { 
  public static void Main(String[] args)
  { // create the object graph
   ArrayList l = new ArrayList();
   for (int x=0; x< 100; x++)
   {
    l.Add (x);
   }
   // create the filestream
   FileStream s = File.Create("foo.bin");
   // create the BinaryFormatter
   BinaryFormatter b = new BinaryFormatter();
   // serialize the graph to the stream
   b.Serialize(s, l);
   s.Close();
  }
 }
六、Deserialize Class and object
Example:
   class DeSerialize
 {
  public static void Main(String[] args)
  { // open the filestream
   FileStream s = File.OpenRead("foo.bin"); 
   // create the formatter
   BinaryFormatter b = new BinaryFormatter(); 
   // deserialize
   ArrayList p = (ArrayList) b.Deserialize(s); s.Close();
   // print out the new object graph
   // see module text for PrintValues’ code
   PrintValues(p);
  }
  }
七、Security Issues
Serialization Handles an Object’s Private Data
If private data is sensitive, consider encrypting the stream before transmitting or saving to a disk
System.Security.Cryptography namespace contains classes to perform cryptography The CryptoStream class can be used to encrypt streams of serialized data

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值