티스토리 뷰
객체를 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;
}
}
'Code > C#' 카테고리의 다른 글
BinaryFormatter.Serialize 사용법 (0) | 2007.12.12 |
---|---|
Generic Method 를 이용한 Serialization (0) | 2007.12.10 |
간단한 DataGridView 사용 예제 (0) | 2007.12.09 |
DateTime & TimeSpan : 두 시간 사이의 간격 계산하기 (0) | 2007.12.09 |
Thread.Sleep() & TimeSpan.FromSeconds() (0) | 2007.12.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Regular Expressions
- iTextSharp
- Service pack
- DotNetMagic
- autohotkey
- java
- tagREADYSTATE
- Rollback Rx
- AxWebBrowser
- ScreenHunter
- registry
- WinAutomation
- AdSense숨기기
- READYSTATE_COMPLETE
- .net framework
- Automation
- iText
- jre
- 유틸리티
- windows
- 스크린캡쳐
- Phalanger
- 애드센스숨기기
- AdSense감추기
- c#
- 애드센스감추기
- Sample Code
- download.com
- Microsoft
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함