日期:2014-05-20 浏览次数:20769 次
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="value">T 对象</param>
/// <param name="filename">文件路径</param>
/// <returns>返回T对象</returns>
public bool Serialize<T>(T value, string filename)
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
bs.Serialize(stream, value);
stream.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public T DeSerialize<T>(string filename)
{
T obj;
if (!System.IO.File.Exists(filename))
return default(T);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open);