⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 C#开发教程 由浅入深 配有实例 是初学者的好帮手
💻 CS
字号:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ImpISerial
{
    [Serializable]
    public class Insect : ISerializable
    {
        private string name;
        private int id;
        public Insect(string name, int id)
        {
            this.name = name;
            this.id = id;
        }
        public override string ToString()
        {
            return String.Format("{0}: {1}", name, id);
        }
        public Insect() {}

        public virtual void GetObjectData(
            SerializationInfo s, StreamingContext c)
        {
            s.AddValue("CommonName", name);
            s.AddValue("ID#", id);
        }
        private Insect(
            SerializationInfo s, StreamingContext c)
        {
            name = s.GetString("CommonName");
            id = s.GetInt32("ID#");
        }
    }

    class ImpISerialApp
    {
        [STAThread]
        static void Main(string[] args)
        {
            Insect i = new Insect("Meadow Brown", 12);
            Stream s = File.Create("Insect.bin");
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(s, i);
            s.Seek(0, SeekOrigin.Begin);
            Insect j = (Insect)b.Deserialize(s);
            s.Close();
            Console.WriteLine(j);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -