You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.6 KiB
C#

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Data;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace Mesnac.Basic
{
/// <summary>
/// 序列化和反序列化处理类
/// </summary>
public class SerializeHandler
{
#region 序列化和反序列化
/// <summary>
/// 序列化指定类型的对象到指定的文件
/// </summary>
/// <typeparam name="T">要序列化的对象类型</typeparam>
/// <param name="obj">要序列化的对象</param>
/// <param name="fileName">保存对象数据的完整文件名</param>
public static void Serialize<T>(T obj, string fileName)
{
lock (fileName)
{
try
{
string dir = Path.GetDirectoryName(fileName); //获取文件路径
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
BinaryFormatter formatter = new BinaryFormatter();
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
formatter.Serialize(fs, obj);
fs.Close();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.Message);
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.StackTrace);
}
}
}
/// <summary>
/// 从指定的文件中反序列化指定类型的对象
/// </summary>
/// <typeparam name="T">反序列化的对象类型</typeparam>
/// <param name="fileName">保存对象数据的文件名</param>
/// <returns>返回反序列化出的对象实例</returns>
public static T Deserialize<T>(string fileName)
{
lock (fileName)
{
try
{
if (!File.Exists(fileName))
{
ICSharpCode.Core.LoggingService<SerializeHandler>.Debug(String.Format("序列化文件{0}不存在!", fileName));
return default(T);
}
else
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
T obj = (T)formatter.Deserialize(fs);
fs.Close();
return obj;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.Message);
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.StackTrace);
return default(T);
}
}
}
/// <summary>
/// 序列化指定类型的对象到指定的Xml文件
/// </summary>
/// <typeparam name="T">要序列化的对象类型</typeparam>
/// <param name="obj">要序列化的对象</param>
/// <param name="fileName">保存对象数据的完整文件名</param>
public static void SerializeXml<T>(T obj, string fileName)
{
lock (fileName)
{
try
{
string dir = Path.GetDirectoryName(fileName); //获取文件路径
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
SoapFormatter formatter = new SoapFormatter();
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
formatter.Serialize(fs, obj);
fs.Close();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.Message);
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.StackTrace);
}
}
}
/// <summary>
/// 从指定的Xml文件中反序列化指定类型的对象
/// </summary>
/// <typeparam name="T">反序列化的对象类型</typeparam>
/// <param name="fileName">保存对象数据的文件名</param>
/// <returns>返回反序列化出的对象实例</returns>
public static T DeserializeXml<T>(string fileName)
{
lock (fileName)
{
try
{
if (!File.Exists(fileName))
{
ICSharpCode.Core.LoggingService<SerializeHandler>.Debug(String.Format("序列化文件{0}不存在!", fileName));
return default(T);
}
else
{
SoapFormatter formatter = new SoapFormatter();
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
T obj = (T)formatter.Deserialize(fs);
fs.Close();
return obj;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.Message);
ICSharpCode.Core.LoggingService<SerializeHandler>.Error(ex.StackTrace);
return default(T);
}
}
}
/// <summary>
/// 把DataTable对象序列化为XML文本字符串
/// </summary>
/// <param name="dt">要序列化的DataTable对象</param>
/// <returns>返回序列化后的XML文本字符串</returns>
public static string SerializeDataTable(DataTable dt)
{
StringWriter strWriter = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(strWriter, dt);
return strWriter.ToString();
}
/// <summary>
/// 把对象序列化为xml字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string SerializeObject<T>(T obj)
{
if (obj != null)
{
StringWriter strWriter = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(strWriter, obj);
return strWriter.ToString();
}
else
{
return String.Empty;
}
}
/// <summary>
/// 把xml字符串反序列化为对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xmlString"></param>
/// <returns></returns>
public static T DeserializeObject<T>(string xmlString)
{
if (!String.IsNullOrEmpty(xmlString))
{
StringReader strReader = new StringReader(xmlString);
XmlSerializer serializer = new XmlSerializer(typeof(T));
T obj = (T)serializer.Deserialize(strReader);
return obj;
}
else
{
return default(T);
}
}
#endregion
}
}