Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C# 객체 직렬화

<시간/>

객체 직렬화를 위해서는 아래 코드를 참조해야 합니다. 여기에서는 BinaryFormatter.Serialize(스트림, 참조) 메서드를 사용하여 샘플 개체를 직렬화했습니다.

여기에 생성자를 설정했습니다 -

public Employee(int id, string name, int salary) {
   this.id = id;
   this.name = name;
   this.salary = salary;
}

이제 파일 스트림을 설정하십시오 -

FileStream fStream = new FileStream("d:\\new.txt", FileMode.OpenOrCreate);
BinaryFormatter bFormat = new BinaryFormatter();

Employee 클래스의 객체 -

Employee emp = new Employee(001, "Jim", 30000);
bFormat.Serialize(fStream, emp);