Generic Method 를 사용하면 상당히 유용한 Serialization 을 구사할 수 있다.
아래 예제는 Generic Method 를 사용하여 어떠한 Type이라도 쉽게 Serialize & Deserialize 할 수 있도록 한 것이다.
이런 것들은 실전에서 유용하게 이용될 수 있다.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace GenericSerializer
{
public static class Serializer
{
public static void Export<T>(string otherFile, T otherT)
{
using (Stream s = File.Open(otherFile, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(s, otherT);
}
}
public static T Import<T>(string otherFile)
{
using (Stream s = File.Open(otherFile, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
object o = bf.Deserialize(s);
T t = (T)o;
return t;
}
}
}
}
아래 예제는 Generic Method 를 사용하여 어떠한 Type이라도 쉽게 Serialize & Deserialize 할 수 있도록 한 것이다.
이런 것들은 실전에서 유용하게 이용될 수 있다.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace GenericSerializer
{
public static class Serializer
{
public static void Export<T>(string otherFile, T otherT)
{
using (Stream s = File.Open(otherFile, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(s, otherT);
}
}
public static T Import<T>(string otherFile)
{
using (Stream s = File.Open(otherFile, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
object o = bf.Deserialize(s);
T t = (T)o;
return t;
}
}
}
}
'Code > C#' 카테고리의 다른 글
| 실행중인 application의 path를 구하는 방법 (0) | 2007.12.15 |
|---|---|
| BinaryFormatter.Serialize 사용법 (0) | 2007.12.12 |
| Serializable Attribute & BinaryFormatter 사용법 (0) | 2007.12.10 |
| 간단한 DataGridView 사용 예제 (0) | 2007.12.09 |
| DateTime & TimeSpan : 두 시간 사이의 간격 계산하기 (0) | 2007.12.09 |