C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C#反序列化

C# 反序列化

在 C# 编程中,反序列化是序列化的逆过程。这意味着您可以从字节流中读取对象。在这里,我们将使用 BinaryFormatter.Deserialize(stream) 方法对流进行反序列化。
C# deserialization

C# 反序列化示例

我们来看看 C# 中反序列化的简单示例。
 
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
class Student
{
    public int rollno;
    public string name;
    public Student(int rollno, string name)
    {
        this.rollno = rollno;
        this.name = name;
    }
}
public class DeserializeExample
{
    public static void Main(string[] args)
    {
        FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);
        BinaryFormatter formatter=new BinaryFormatter();
        Student s=(Student)formatter.Deserialize(stream);
        Console.WriteLine("Rollno: " + s.rollno);
        Console.WriteLine("Name: " + s.name);
        stream.Close();
    }
}
输出:
 Rollno: 101
Name: sonoo
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4