首页 > 代码库 > C# 序列化
C# 序列化
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 序列化{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Person p = new Person(); p.Name ="张三"; p.Age =18; p.Gender = true; using (System.IO.FileStream file = new FileStream("person.dat", FileMode.Create, FileAccess.Write)) { //System.Runtime.Serialization.Formatters.Binary; BinaryFormatter b = new BinaryFormatter(); b.Serialize(file, p); //序列化 } MessageBox.Show("ok"); } private void button2_Click(object sender, EventArgs e) { using (System.IO.FileStream file = new FileStream("person.dat", FileMode.Open, FileAccess.Read)) { BinaryFormatter b = new BinaryFormatter(); Person p = (Person)b.Deserialize(file); //反序列化 MessageBox.Show(p.Name); } } } //程序集“序列化, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“序列化.Person”未标记为可序列化。 [Serializable] public class Person { private string name; private int age; private bool gender; public bool Gender { get { return gender; } set { gender = value; } } public int Age { get { return age; } set { age = value; } } public string Name { get { return name; } set { name = value; } } }}
C# 序列化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。