C#序列化和反序列化
1 、给类加上[Serializable]属性 表示这个类是可以序列化
[csharp]
[Serializable]
public class Userinfo
{
public string uname { get; set; }
public string upass {get;set;}
public int IsSave { get; set; }
}
2、将对象序列化到文件中
[csharp]
//序列化
FileStream fs = new FileStream(@"D:\serializeDem.dat", FileMode.Create);
Userinfo model = new Userinfo() { uname= "admin",upass="admin" IsSave = 1 };
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, model);
fs.Close();
3、从文件反序列化为对象
[csharp]
//反序列化
FileStream fs = new FileStream(@"D:\serializeDem.dat", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
Userinfo model = bf.Deserialize(fs) as Userinfo;
摘自 欧阳小燕
补充:软件开发 , C# ,