ASP.NET之:序列化

1 什么事序列化?为什么需要序列化

有两个最重要的原因促使对序列化的使用:一个原因是将对象的状态保持在存储媒体中,以便可以在以后重新创建精确的副本;

另一个原因是通过值将对象从一个应用程序域发送到另一个应用程序域中。
  
  
序列化的是对象的状态   
  
也就是对象数据成员的值和方法是没有关系的

跨应用程序域通信时,以及用WEB服务时 要用到序列化   

2 .1 序列化的方式

一:BinaryFormatter序列化

序列化简单点来理解就是把内存的东西写到硬盘中,当然也可以写到内存中(这个内容我会在后面写一个例子).而反序列化就是从硬盘中把信息读到内存中.就这么简单,呵呵,现在来看下面的例子吧!

在这篇文章中我将使用BinaryFormatter序列化类Person作为例子,希望大家能从例子中深刻体会什么是序列化.

 /// <summary>
    /// ClassToSerialize 的摘要说明
    /// </summary>
    [Serializable]
    public class Person
    {
        public int id;
        public string name;
        //[NonSerialized]
        public string Sex;
    }

在类的上面增加了属性:Serializable.(如果不加这个属性,将抛出SerializationException异常).

通过这个属性将Book标志为可以序列化的.当然也有另一种方式使类Book可以序列化,那就是实行ISerializable接口了.在这里大家要注意了:Serializable属性是不能被继承的咯!!!

如果你不想序列化某个变量,该怎么处理呢?很简单,在其前面加上属性[NonSerialized] .比如我不想序列化

好了,现在就告诉大家怎么实现序列化:

我们使用namespace:

using System;

using System.IO;

using System.RunTime.Serialization.Formatters.Binary;

 序列化:

 public void SerializeBinaryNow()
        {
            Person c = new Person();
            c.id = 1;
            c.name = "zhangjunwei";
            c.Sex = "male";
            using (FileStream fileStream = new FileStream("E:\\proj\\File\\temp.dat", FileMode.Create))
            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(fileStream, c);
            }
        }

反序列化:

        public void DeSerializeBinaryNow()
        {
            Person c = new Person();
            using (FileStream fileStream = new FileStream("E:\\proj\\File\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BinaryFormatter b = new BinaryFormatter();
                c = b.Deserialize(fileStream) as Person;
                Response.Write("id=" + c.id);
                Response.Write("</br>");
                Response.Write("name=" + c.name);
                Response.Write("</br>");
                Response.Write("sex=" + c.Sex);
            }
        }

注意一定不要忘了: using   System.Runtime.Serialization.Formatters.Binary;  
   
命名空间。

2.2SOAP 序列化 XML 序列化

SOAP:

SoapFormatter b = new SoapFormatter();

XML:

XmlSerializer xs = new XmlSerializer(typeof(Person));

3 序列化方便对象的传输及储存;  
 
它能将对象转成如XML/Bit流;  
 
它是Session(进程外/SqlServer模式),ViewState,WebService,Remoting等的基础。

收录四:

例如学生类  
  public   class   Student  
  {  
  public   int   id;  
  public   string   name;  
  public   bool   sex;  
  public   DateTime   birthday;  
  ...  
  }  
   
  //  
序列化为byte[]  
  MemoryStream   fs   =   new   MemoryStream();  
  byte[]   tmp   =   null;  
  BinaryFormatter   formatter   =   new   BinaryFormatter();  
  formatter.Serialize(fs,   student);  
  tmp   =   fs.ToArray();  
 
tmp作为bianry数据存到数据库  
   
  //  
反序列化直接生成Student  
  MemoryStream   fs   =   new   MemoryStream();  
  Student   student   =   null;  
  fs   =   new   MemoryStream(tmp);  
  fs.Position   =   0;  
  BinaryFormatter   formatter   =   new   BinaryFormatter();  
  student   =   formatter.Deserialize(fs);  


  

原文地址:https://www.cnblogs.com/zjwei55/p/2138341.html