public class Book
{
string name
floatprice
string author public Book(string bookname, float bookprice, string bookauthor)
{
name = bookname
price = bookprice
author = bookauthor
}
} 在类的上面增加了属性:Serializable.(如果不加这个属性,将抛出SerializationException异常). 通过这个属性将Book标志为可以序列化的.当然也有另一种方式使类Book可以序列化,那就是实行ISerializable接口了.在这里要注意了:Serializable属性是不能被继承的咯!!! 如果你不想序列化某个变量,该怎么处理呢?很简单,在其前面加上属性[NonSerialized] .比如我不想序列化 string author那我只需要 [NonSerialized] string author好了,现在请看怎么实现序列化: 我们使用namespace: using Systemusing System.IOusing System.Runtime.Serialization.Formatters.Binary 首先创建Book实例,like this:Book book = new Book("Day and Night", 30.0f, "Bruce")接着当然要创建一个文件了,这个文件就是用来存放我们要序列化的信息了. FileStream fs = new FileStream(@"C:\book.dat", FileMode.Create)序列化的实现也很简单,like this: BinaryFormatter formatter = new BinaryFormatter()
formatter.Serialize(fs, book)很简单吧!现在我列出整个原代码,包括反序列化. static void Main(string[] args)
{
Book book = new Book("Day and Night", 30.0f, "Bruce") using(FileStream fs = new FileStream(@"C:\book.dat", FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter()
formatter.Serialize(fs, book)
}book = null using(FileStream fs = new FileStream(@"C:\book.dat", FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter()
book = (Book)formatter.Deserialize(fs)//在这里要注意咯,他的返回值是object
}
}
C#可以序列化,一般可以转成[二进制序列化][SOAP序列化][XML 序列化],就举个XML的例子吧。写了一个类并序列化和反序。
1、我写的是控制台程序
2、要引用using System.Xml.Serializationusing System.IO
3、Person类中,Address属性我定义了忽略,他不会被序列化,所以你打开XML是看不到这个字段的。
4、文件我写死了,是D盘的1234.xml,你可以改。
=====代码====
[Serializable]public class Person
{
/// <summary>
/// 姓名
/// </summary>
public string Name { get set }
/// <summary>
/// 年龄
/// </summary>
public int Age { get set }
/// <summary>
/// 地址,属性指这个字段不序列化
/// </summary>
[XmlIgnore]
public string Address { get set }
}
上面是个类,要序列化,就要写特性[Serializable],下面是我写的两个方法
/// <summary>/// 序列化
/// </summary>
/// <param name="type"></param>
/// <param name="obj"></param>
static void CreateSerialize(Type type, object obj)
{
XmlSerializer xs = new XmlSerializer(type)
Stream stream = new FileStream(@"d:\1234.XML", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
xs.Serialize(stream, obj)
stream.Close()
}
/// <summary>
/// 反序
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
static object XMLDeserialize(Type type)
{
XmlSerializer xs = new XmlSerializer(type)
Stream stream = new FileStream(@"d:\\1234.XML", FileMode.Open, FileAccess.Read, FileShare.Read)
var str = xs.Deserialize(stream)
stream.Close()
return str
}
你可以在主程序中Main()中调用上面的方法,我给了例子。
//建立几个对象List<Person> list = new List<Person>()
{
new Person(){ Name = "张三", Age = 18, Address = "A小区" },
new Person(){ Name = "李四", Age = 21, Address = "B小区" },
new Person(){ Name = "王五", Age = 23, Address = "C小区" },
new Person(){ Name = "赵六", Age = 17, Address = "D小区" },
new Person(){ Name = "钱七", Age = 22, Address = "E小区" },
}
foreach (var obj in list)
{
Console.WriteLine("Name = " + obj.Name + "\tAge = " + obj.Age + "\t地址 = " + (String.IsNullOrEmpty(obj.Address) ? "空" : obj.Address))
}
Console.WriteLine("序列化开始....")
CreateSerialize(typeof(List<Person>), list)
Console.WriteLine("序列化结束....")
Console.WriteLine("反序列化开始....")
var dList = XMLDeserialize(typeof(List<Person>)) as List<Person>
foreach (var obj in dList)
{
Console.WriteLine("Name = " + obj.Name + "\tAge = " + obj.Age + "\t地址 = " + (String.IsNullOrEmpty(obj.Address) ? "空" : obj.Address))
}
Console.WriteLine("反序列化结束....")
Console.ReadKey()
这只是个简单的例子。
在类定义前写上[Serializable()]就可以比如:
[Serializable()]
public class User
{
…………
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)