C# 属性

C# 抽象

C# 字符串

C# 泛型

C# 杂项

C# 新特性

C# Deserialization 反序列化

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

C# 反序列化

C# 反序列化示例

让我们看一下 C# 中反序列化的简单示例。

例子 (Example)

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


  • 使用社交账号登录,本站支持
全部评论(0)