C# での逆シリアル化

王林
リリース: 2024-09-03 15:25:14
オリジナル
374 人が閲覧しました

次の記事では、C# での逆シリアル化の概要を説明します。まずはシリアル化のプロセスを見てみましょう。シリアル化は、オブジェクトをストリームに書き込んでメモリ、データベース、またはファイルに保存できるように、オブジェクトをフォームに変換するプロセスです。その主な目的は、オブジェクトの状態を保存することです。

ここで、逆シリアル化はシリアル化の逆のプロセスです。これは、バイト ストリームを読み取ったり、オブジェクトに変換してメモリにロードできるようにするプロセスです。このプロセスにより、必要なときにいつでもオブジェクトを再構築できるようになります。

説明付きの構文:

BinaryFormatter を使用した逆シリアル化の構文は次のとおりです。

FileStream fileStream = new FileStream(filePath, FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
ClassName objectName = (ClassName)binaryformatter.Deserialize(fileStream);
ログイン後にコピー

上記の構文では、まず、オブジェクトを再構築するための情報を取得するファイルのパス (filePath) を指定して、FileStream のオブジェクト (fileStream) を作成しました。この後、BinaryFormatterのオブジェクトを作成しました。 BinaryFormatter は、System.Runtime.Serialization.Formatters.Binary 名前空間の下に存在するクラスで、オブジェクトのシリアル化と逆シリアル化に使用されます。次に、BinaryFormatter の Deserialize() メソッドを使用してオブジェクトを逆シリアル化しました。このメソッドは、FileStream のオブジェクトを入力として受け取り、ClassName 型のオブジェクトに変換して objectName に格納したオブジェクトを返します。

C# では逆シリアル化はどのように機能しますか?

C# で逆シリアル化するには、まずコードに System.IO 名前空間をインポートして、オブジェクトの再構築に使用されるデータを含むファイルを開く必要があります。次に、オブジェクトのシリアル化と逆シリアル化を担当する BinaryFormatter クラスを操作するために、System.Runtime.Serialization.Formatters.Binary 名前空間をインポートする必要があります。

「Name」と「RollNo」という 2 つのプロパティを持つ「Student」というクラスがあるとします。シリアル化プロセスを使用して、クラス「Student」データ プロパティをファイルに書き込みます。次に、BinaryFormatter クラスの Deserialize() メソッドを呼び出すことで、そのファイルからデータを読み取り、クラス「Student」のオブジェクトを再構築できます。これをオブジェクトの逆シリアル化と呼びます。

BinaryFormatter を使用して C# でオブジェクトを逆シリアル化する手順は次のとおりです。

  • まず、シリアル化された情報またはデータを読み取るためのストリーム オブジェクトを作成する必要があります。
  • 次に、BinaryFormatter クラスのオブジェクトを作成します。
  • その後、BinaryFormatter クラスの Deserialize() メソッドを呼び出してオブジェクトを逆シリアル化します。このメソッドは、適切な型にキャストできるオブジェクトを返します。

C# には、次の 3 種類のシリアル化があります。

  • バイナリシリアル化
  • XML シリアル化
  • JSON シリアル化

したがって、実行されたシリアル化に応じて、3 つの方法でオブジェクトを逆シリアル化できます。バイナリのシリアル化と逆シリアル化には、上で説明したように BinaryFormatter クラスを使用します。 XML のシリアル化と逆シリアル化には、XMLSerializer クラスを使用します。 JSON のシリアル化と逆シリアル化には、JsonSerializer クラスを使用します。

C# でのシリアル化と逆シリアル化の図的表現:

C# での逆シリアル化

C# での逆シリアル化の例

以下に例を示します:

例 #1

バイナリのシリアル化と逆シリアル化を示す例。

コード:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApp4
{
class Program
{
public static void SerializingData()
{
string str = "Hello world!";
FileStream fileStream = new FileStream(@"E:\Content\content.txt",
FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, str);
fileStream.Close();
}
public static void DeserializingData()
{
FileStream fileStream = new FileStream(@"E:\Content\content.txt",
FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
string content = "";
content = (string)binaryFormatter.Deserialize(fileStream);
fileStream.Close();
Console.WriteLine("Deserialized data: ");
Console.WriteLine(content);
}
static void Main(string[] args)
{
SerializingData();
DeserializingData();
Console.ReadLine();
}
}
}<strong> </strong>
ログイン後にコピー

出力:

C# での逆シリアル化

例 #2

カスタム クラスのバイナリ シリアル化と逆シリアル化を示す例。

コード:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApp4
{
[Serializable]
public class Student
{
public int RollNo;
public string Name;
public string Address;
public Student(int rollNo, string name, string address)
{
RollNo = rollNo;
Name = name;
Address = address;
}
}
public class Program
{
public static void SerializingData()
{
Student student = new Student(1, "Sneha", "Kasarwadi, Pune");
//creating file to store data
FileStream fileStream = new FileStream(@"E:\Content\Student.txt",
FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
//serializing data using Serialize() method
binaryFormatter.Serialize(fileStream, student);
fileStream.Close();
}
public static void DeserializingData()
{
Student student;
//opening file to read data
FileStream fileStream = new FileStream(@"E:\Content\Student.txt",
FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
//creating object to store deserialized data
student = (Student)binaryFormatter.Deserialize(fileStream);
int rollNo = student.RollNo;
string name = student.Name;
string address = student.Address;
fileStream.Close();
//displaying deserialized data
Console.WriteLine("Deserialized data: ");
Console.WriteLine("Roll No = " + rollNo);
Console.WriteLine("Student Name = " + name);
Console.WriteLine("Student Address = " + address);
}
public static void Main(string[] args)
{
SerializingData();
DeserializingData();
Console.ReadLine();
}
}
}
ログイン後にコピー

出力:

C# での逆シリアル化

例 #3

カスタム クラスの XML シリアル化と逆シリアル化を示す例。

コード:

using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApp4
{
[Serializable]
public class Student
{
public int RollNo { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public Student()
{
RollNo = 0;
Name = "N/A";
Address = "N/A";
}
}
public class Program
{
public static void SerializingData(Student student)
{
//creating file to store data.
FileStream fileStream = new FileStream(@"E:\Content\Student.txt",
FileMode.Create);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Student));
//calling serialize() method to serialize data to file
xmlSerializer.Serialize(fileStream, student);
fileStream.Close();
}
public static void DeserializingData()
{
//opening file to read data
FileStream fileStream = new FileStream(@"E:\Content\Student.txt",
FileMode.Open);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Student))
//calling Deserialize() to deserialize data from the file
Student student = (Student)xmlSerializer.Deserialize(fileStream);
fileStream.Close();
Console.WriteLine("Deserialized data: ");
Console.WriteLine("Student Roll No = " + student.RollNo);
Console.WriteLine("Student Name = " + student.Name);
Console.WriteLine("Student Address = " + student.Address);
}
public static void Main(string[] args)
{
Student student = new Student();
Console.WriteLine("Enter student Roll No");
student.RollNo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter student name");
student.Name = Console.ReadLine();
Console.WriteLine("Enter student address");
student.Address = Console.ReadLine();
SerializingData(student);
DeserializingData();
Console.ReadLine();
}
}
}
ログイン後にコピー

出力:

C# での逆シリアル化

結論

逆シリアル化は、以前にシリアル化されたバイト シーケンスからオブジェクトを再構築するプロセスです。これにより、必要なときにいつでもオブジェクトを回復できるようになります。シリアル化の逆のプロセスです。 BinaryFormatter クラスの Deserialize() メソッドは、バイナリ ストリームからの逆シリアル化に使用されます。

以上がC# での逆シリアル化の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!