序列化和反序列化我们可能经常会听到,其实通俗一点的解释,序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用。
序列化和反序列化最主要的作用有:
1、在进程下次启动时读取上次保存的对象的信息
2、在不同的AppDomain或进程之间传递数据
3、在分布式应用系统中传递数据
引用:http://www.cnblogs.com/ejiyuan/archive/2009/01/21/1379256.html
1.给要序列化的类加上Serializable特性
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
//序列化靠反射实现
namespace SerializeExsample
{
//序列化就是将对象转换为容易传输的格式在各个程序之间共享
//必须给可序列化的对象加上Serializable特性
[Serializable]
public class User
{
//[NonSerialized]只能在字段上加上该特性表示不会序列化字段
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
//Serailize
User user = new User { Name = "Sonven", Age = 35 };
BinaryFormatter format = new BinaryFormatter();
Stream stream = new FileStream("temp.t", FileMode.Create);
format.Serialize(stream, user);//参数为一个Stream,object
stream.Dispose();
//Deserialize
stream = new FileStream("temp.t", FileMode.Open, FileAccess.Read);
User newuser =format.Deserialize(stream) as User; //Deserialize
if (newuser != null) Console.WriteLine("Deserialize user:Name{0},Age{1}", newuser.Name, newuser.Age);
stream.Dispose();
File.Delete("temp.t");//Delete Temp Data
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
//序列化靠反射实现
namespace SerializeExsample
{
//序列化就是将对象转换为容易传输的格式在各个程序之间共享
//必须给可序列化的对象加上Serializable特性
[Serializable]
public class User
{
//[NonSerialized]只能在字段上加上该特性表示不会序列化字段
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
//Serailize
User user = new User { Name = "Sonven", Age = 35 };
BinaryFormatter format = new BinaryFormatter();
Stream stream = new FileStream("temp.t", FileMode.Create);
format.Serialize(stream, user);//参数为一个Stream,object
stream.Dispose();
//Deserialize
stream = new FileStream("temp.t", FileMode.Open, FileAccess.Read);
User newuser =format.Deserialize(stream) as User; //Deserialize
if (newuser != null) Console.WriteLine("Deserialize user:Name{0},Age{1}", newuser.Name, newuser.Age);
stream.Dispose();
File.Delete("temp.t");//Delete Temp Data
Console.ReadKey();
}
}
}
2.实现序列化接口
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializeExsample.Custorm
{
[Serializable]
public class User:ISerializable
{
public string Name { get; set; }
public int Age { get; set; }
public User() { }
//反序列化
//此构造函数用于创建反序列化后的对象
public User(SerializationInfo info, StreamingContext context)
{
//SerializationInfo的Value键值区分大小些,如果Age改为age将抛出错误
Age =(int) info.GetValue("Age", typeof(int));
// Name = info.GetValue("Name", typeof(string)) as string;
}
//序列化,在里面添加要被序列化的属性
//实现ISerializable的GetObjectData方法
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Age", this.Age);
//info.AddValue("Name", this.Name);
}
}
class Program
{
static void Main()
{
User user=new User{Name="Jack",Age=22};
//Serialize
BinaryFormatter format = new BinaryFormatter();
Stream stream = new FileStream("temp.t", FileMode.Create);
format.Serialize(stream, user);
stream.Dispose();
//Deserialize
stream = new FileStream("temp.t", FileMode.Open, FileAccess.Read);
user = format.Deserialize(stream) as User;
stream.Dispose();
if (user != null) Console.WriteLine("Deserialized user Name:{0},Age:{1}", user.Name, user.Age);
Console.ReadKey();
//反序列后的对象Name为null,Age为22
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializeExsample.Custorm
{
[Serializable]
public class User:ISerializable
{
public string Name { get; set; }
public int Age { get; set; }
public User() { }
//反序列化
//此构造函数用于创建反序列化后的对象
public User(SerializationInfo info, StreamingContext context)
{
//SerializationInfo的Value键值区分大小些,如果Age改为age将抛出错误
Age =(int) info.GetValue("Age", typeof(int));
// Name = info.GetValue("Name", typeof(string)) as string;
}
//序列化,在里面添加要被序列化的属性
//实现ISerializable的GetObjectData方法
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Age", this.Age);
//info.AddValue("Name", this.Name);
}
}
class Program
{
static void Main()
{
User user=new User{Name="Jack",Age=22};
//Serialize
BinaryFormatter format = new BinaryFormatter();
Stream stream = new FileStream("temp.t", FileMode.Create);
format.Serialize(stream, user);
stream.Dispose();
//Deserialize
stream = new FileStream("temp.t", FileMode.Open, FileAccess.Read);
user = format.Deserialize(stream) as User;
stream.Dispose();
if (user != null) Console.WriteLine("Deserialized user Name:{0},Age:{1}", user.Name, user.Age);
Console.ReadKey();
//反序列后的对象Name为null,Age为22
}
}
}
3.使用XMLSerializer
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace SerializeExsample.Xml
{
public class User
{
public string Name { get; set; }
public int Age{get;set;}
[XmlIgnore] //忽略对属性序列化
public DateTime RegDate { get; set; }
}
class Program
{
static void Main()
{
User user=new User{Name="Sonven",Age=35,RegDate=DateTime.Now};
//Serialize
XmlSerializer serializer = new XmlSerializer(typeof(User));//Param:Type要被序列化的对象类型
Stream stream=new FileStream("temp.xml",FileMode.Create);
serializer.Serialize(stream, user);
stream.Dispose();
/***********************************************
* 因为RegDate应用了XmlIgnore特性,xml文件不包含
* 序列化将生成XML文件类容如下,
<?xml version="1.0"?>
<User>
<Name>Sonven</Name>
<Age>35</Age>
</User>
* ******************************************/
//Deserialize
stream = new FileStream("temp.xml",FileMode.Open,FileAccess.Read);
user = serializer.Deserialize(stream) as User;
stream.Dispose();
if (user != null) Console.WriteLine("Deserialize User Name:{0},Age:{1}", user.Name, user.Age);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace SerializeExsample.Xml
{
public class User
{
public string Name { get; set; }
public int Age{get;set;}
[XmlIgnore] //忽略对属性序列化
public DateTime RegDate { get; set; }
}
class Program
{
static void Main()
{
User user=new User{Name="Sonven",Age=35,RegDate=DateTime.Now};
//Serialize
XmlSerializer serializer = new XmlSerializer(typeof(User));//Param:Type要被序列化的对象类型
Stream stream=new FileStream("temp.xml",FileMode.Create);
serializer.Serialize(stream, user);
stream.Dispose();
/***********************************************
* 因为RegDate应用了XmlIgnore特性,xml文件不包含
* 序列化将生成XML文件类容如下,
<?xml version="1.0"?>
<User>
<Name>Sonven</Name>
<Age>35</Age>
</User>
* ******************************************/
//Deserialize
stream = new FileStream("temp.xml",FileMode.Open,FileAccess.Read);
user = serializer.Deserialize(stream) as User;
stream.Dispose();
if (user != null) Console.WriteLine("Deserialize User Name:{0},Age:{1}", user.Name, user.Age);
Console.ReadKey();
}
}
}