class1.cs
来自「C#开发教程 由浅入深 配有实例 是初学者的好帮手」· CS 代码 · 共 54 行
CS
54 行
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 + =
减小字号Ctrl + -
显示快捷键?