• 对象序列化笔记


     

    using System;

    using System.Xml.Serialization; //Xml序列化时引入

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Web.Script.Serialization; //json序列化时引入

    using System.IO;

    using System.Runtime.Serialization.Formatters.Binary; //二进制序列化时引入

     

     

    namespace ConsoleApplication6

    {

    class Program

    {

    static void Main(string[] args)

    {

    Person p1 = new Person();

    p1.Name = "池少舫";

    p1.Age = 10;

    p1.Email = "csf@chi.com";

     

    //json序列化

    //JavaScriptSerializer jss = new JavaScriptSerializer();

    //string str = jss.Serialize(p1);

    //for (int i = 0; i < str.Length; i++)

    //{

    // File.WriteAllText("person.json", str);

    //}

     

    //xml序列化

    //XmlSerializer xmls = new XmlSerializer(typeof(Person));

    //FileStream fs = new FileStream("person.xml", FileMode.Create);

    //xmls.Serialize(fs, p1);

     

    //二进制序列化

    BinaryFormatter bf = new BinaryFormatter();

    using (FileStream fs = new FileStream("person.bin",FileMode.Create))

    {

    bf.Serialize(fs, p1);

    }

     

    Console.WriteLine("ok");

    Console.ReadKey();

     

    }

    }

     

    [Serializable]

    public class Person

    {

    public string Name;

    public int Age;

    public string Email;

    }

    }

  • 相关阅读:
    socket-重叠模型(overlap)
    ssh 免密登陆
    安装google 框架
    为什么不同网段的ip 不能直接通信
    python中的import,reload,以及__import__
    C Runtime Library、C  Runtime
    SQLite3 C/C++ 开发接口简介
    mysql添加索引语句
    mysql 字段左右补0
    @Transactional注解的失效场景
  • 原文地址:https://www.cnblogs.com/HarryChis/p/13634252.html
Copyright © 2020-2023  润新知