C#의 이진 직렬화 및 역직렬화란 무엇이며 C#에서 이진 직렬화를 구현하는 방법은 무엇입니까?

PHPz
풀어 주다: 2023-09-05 15:53:02
앞으로
1338명이 탐색했습니다.

C#의 이진 직렬화 및 역직렬화란 무엇이며 C#에서 이진 직렬화를 구현하는 방법은 무엇입니까?

객체를 읽을 수 없는 바이너리 형식으로 변환하는 것을 바이너리 직렬화라고 합니다.

바이너리 형식을 다시 읽을 수 있는 형식으로 변환하는 것을 역직렬화라고 하나요?

C#에서 바이너리 직렬화를 구현하려면 System.Runtime.Serialization.Formatters.Binary Assembly 라이브러리를 사용해야 합니다.

BinaryFormatter 클래스의 객체를 생성하고 클래스 내부에서 serialize 메서드를 사용하세요.

Example

Serialize an Object to Binary
[Serializable]
public class Demo {
   public string ApplicationName { get; set; } = "Binary Serialize";
   public int ApplicationId { get; set; } = 1001;
}
class Program {
   static void Main()    {
      Demo sample = new Demo();
      FileStream fileStream = new FileStream(@"C:\Temp\Questions.dat", FileMode.Create);
      BinaryFormatter formatter = new BinaryFormatter();
      formatter.Serialize(fileStream, sample);
      Console.ReadKey();
   }
}
로그인 후 복사

Output

ÿÿÿÿ
로그인 후 복사

AConsoleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null ConsoleApp.Demok__BackingField - k__BackingField 바이너리 직렬화

Example

Converting back from Binary to Object
[Serializable]
public class Demo {
   public string ApplicationName { get; set; }
   public int ApplicationId { get; set; }
}
class Program {
   static void Main()    {
      FileStream fileStream = new FileStream(@"C:\Temp\Questions.dat ", FileMode.Open);
      BinaryFormatter formatter = new BinaryFormatter();
      Demo deserializedSampledemo = (Demo)formatter.Deserialize(fileStream);
      Console.WriteLine($"ApplicationName { deserializedSampledemo.ApplicationName} --- ApplicationId       { deserializedSampledemo.ApplicationId}");
      Console.ReadKey();
   }
}
로그인 후 복사

Output

rreee

위 내용은 C#의 이진 직렬화 및 역직렬화란 무엇이며 C#에서 이진 직렬화를 구현하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!