Code/C#
Serializable Attribute & BinaryFormatter 사용법
Hide Code
2007. 12. 10. 09:36
객체를 serialize 하여 파일에 저장하는 방법은 아래와 같다.
serialize 하려는 클래스에 [Serializable] Attribute 를 붙인다.
Stream 으로 serialize 파일을 연다.
BinaryFormatter.Serialize 로 serialize 된 객체를 파일에 저장한다.
BinaryFormatter.Deserialize 를 사용하면 serialize 되어 파일에 저장된 객체를 불러올 수 있다.
아래 예제를 살펴보자.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializableAttribute0001
{
class Program
{
static void Main(string[] args)
{
// Store some data to disk...
Serialize();
// And read it back in again...
DeSerialize();
}
public static void Serialize()
{
// Construct a person object
Person me = new Person();
// Set the data that is to be serialized
me.Age = 38;
me.Weight = 70;
Console.WriteLine("Serialized Person age: {0} weight: {1}", me.Age, me.Weight);
// Create a disk file to store the object to...
using (Stream s = File.Open("Me.dat", FileMode.Create))
{
// Use a BinaryFormatter to write the object to the FileStream
BinaryFormatter bf = new BinaryFormatter();
// Serialize the object
bf.Serialize(s, me);
}
}
public static void DeSerialize()
{
// Open the file this time
using (Stream s = File.Open("Me.dat", FileMode.Open))
{
// Use a BinaryFormatter to read object(s) from the FileStream
BinaryFormatter bf = new BinaryFormatter();
// Serialize the object
object o = bf.Deserialize(s);
// Ensure it is of the correct type...
Person p = o as Person;
if (p != null)
Console.WriteLine("DeSerialized Person age: {0} weight: {1}", p.Age, p.Weight);
}
}
}
[Serializable]
public class Person
{
public int Age;
public int Weight;
}
}
serialize 하려는 클래스에 [Serializable] Attribute 를 붙인다.
Stream 으로 serialize 파일을 연다.
BinaryFormatter.Serialize 로 serialize 된 객체를 파일에 저장한다.
BinaryFormatter.Deserialize 를 사용하면 serialize 되어 파일에 저장된 객체를 불러올 수 있다.
아래 예제를 살펴보자.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializableAttribute0001
{
class Program
{
static void Main(string[] args)
{
// Store some data to disk...
Serialize();
// And read it back in again...
DeSerialize();
}
public static void Serialize()
{
// Construct a person object
Person me = new Person();
// Set the data that is to be serialized
me.Age = 38;
me.Weight = 70;
Console.WriteLine("Serialized Person age: {0} weight: {1}", me.Age, me.Weight);
// Create a disk file to store the object to...
using (Stream s = File.Open("Me.dat", FileMode.Create))
{
// Use a BinaryFormatter to write the object to the FileStream
BinaryFormatter bf = new BinaryFormatter();
// Serialize the object
bf.Serialize(s, me);
}
}
public static void DeSerialize()
{
// Open the file this time
using (Stream s = File.Open("Me.dat", FileMode.Open))
{
// Use a BinaryFormatter to read object(s) from the FileStream
BinaryFormatter bf = new BinaryFormatter();
// Serialize the object
object o = bf.Deserialize(s);
// Ensure it is of the correct type...
Person p = o as Person;
if (p != null)
Console.WriteLine("DeSerialized Person age: {0} weight: {1}", p.Age, p.Weight);
}
}
}
[Serializable]
public class Person
{
public int Age;
public int Weight;
}
}