c#中list对象的序列化和反序列化



List list=new List();

注意:在c#序列化和反序列化中,在定义Class类时,需要在前面加[Serializable],如:[Serializable] public class Class{}

序列化:

SaveFileDialog save = new SaveFileDialog();
            save.FileName = "画图";
            if (save.ShowDialog() == DialogResult.OK)
            {
                FileStream fileStream = new FileStream(save.FileName + ".gra", FileMode.Create);
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(fileStream, list);
                fileStream.Close();
            }

反序列化:

OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                FileStream fileStream = new FileStream(open.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryFormatter b = new BinaryFormatter();
                list = (List)b.Deserialize(fileStream);
                fileStream.Close();
            }

你可能感兴趣的:(C#语言编程技巧,序列化反序列化,c#,class,类,对象)