/// <summary> /// 将对象实例序列化成字符串 /// </summary> /// <param name="graph">序列化对象</param> /// <param name="formatterType">序列化格式编码</param> /// <exception cref="ArgumentNullException"><paramref name="graph"/>为null</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static string SerializeObject(object graph, SerializationFormatterType formatterType) #endif { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (SerializationFormatterType.Xml == formatterType) { return(XmlHelper.SerializeObject(graph)); } if (SerializationFormatterType.Json == formatterType) { return(JsonHelper.SerializeObject(graph)); } #if HAVE_IFORMATTER var formatter = GetFormatter(formatterType, binder); using (MemoryStream memoryStream = new MemoryStream()) { formatter.Serialize(memoryStream, graph); byte[] arrGraph = memoryStream.ToArray(); return(Convert.ToBase64String(arrGraph)); } #endif throw new NotSupportedException($"{nameof(formatterType)}为不支持的序列化类型"); }
/// <summary> /// 将对象实例序列化到文件里 /// </summary> /// <param name="graph">序列化对象</param> /// <param name="formatterType">序列化格式编码</param> /// <param name="path">序列化文件路径</param> /// <exception cref="ArgumentNullException"><paramref name="graph"/>为null</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static bool SerializeObjectToFile(object graph, SerializationFormatterType formatterType, string path) #endif { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (SerializationFormatterType.Xml == formatterType) { return(XmlHelper.SerializeObjectToFile(graph, path)); } if (SerializationFormatterType.Json == formatterType) { return(JsonHelper.SerializeObjectToFile(graph, path)); } #if HAVE_IFORMATTER var formatter = GetFormatter(formatterType, binder); using (FileStream fileStream = new FileStream(path, FileMode.Create)) { formatter.Serialize(fileStream, graph); fileStream.Flush(); return(true); } #endif throw new NotSupportedException($"{nameof(formatterType)}为不支持的序列化类型"); }
/// <summary> /// 将字符串反序列化成对象实例 /// </summary> /// <typeparam name="T">反序列化类型</typeparam> /// <param name="serializedGraph">反序列化字符串</param> /// <param name="formatterType">序列化格式编码</param> /// <exception cref="ArgumentException"><paramref name="serializedGraph"/>为null或空白字符串</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static T DeserializeObject <T>(string serializedGraph, SerializationFormatterType formatterType) #endif { if (string.IsNullOrWhiteSpace(serializedGraph)) { throw new ArgumentException($"{nameof(serializedGraph)}为null或空白字符串"); } if (SerializationFormatterType.Xml == formatterType) { return(XmlHelper.DeserializeObject <T>(serializedGraph)); } if (SerializationFormatterType.Json == formatterType) { return(JsonHelper.DeserializeObject <T>(serializedGraph)); } #if HAVE_IFORMATTER var formatter = GetFormatter(formatterType, binder); if (binder != null) { formatter.Binder = binder; } byte[] arrGraph = Convert.FromBase64String(serializedGraph); using (MemoryStream memoryStream = new MemoryStream(arrGraph)) { return((T)formatter.Deserialize(memoryStream)); } #endif throw new NotSupportedException($"{nameof(formatterType)}为不支持的序列化类型"); }
/// <summary> /// 将文件内容反序列化成对象实例 /// </summary> /// <typeparam name="T">反序列化类型</typeparam> /// <param name="path">反序列化文件路径</param> /// <param name="formatterType">序列化格式编码</param> /// <exception cref="FileNotFoundException"><paramref name="path"/>指定的文件不存在</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static T DeserializeObjectFromFile <T>(string path, SerializationFormatterType formatterType) #endif { if (!File.Exists(path)) { throw new FileNotFoundException($"{nameof(path)}指定的文件不存在", path); } if (SerializationFormatterType.Xml == formatterType) { return(XmlHelper.DeserializeObjectFromFile <T>(path)); } if (SerializationFormatterType.Json == formatterType) { return(JsonHelper.DeserializeObjectFromFile <T>(path)); } #if HAVE_IFORMATTER var formatter = GetFormatter(formatterType, binder); if (binder != null) { formatter.Binder = binder; } using (FileStream fileStream = new FileStream(path, FileMode.Open)) { return((T)formatter.Deserialize(fileStream)); } #endif throw new NotSupportedException($"{nameof(formatterType)}为不支持的序列化类型"); }
/// <summary> /// 反序列化对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serializedGraph"></param> /// <param name="formatterType"></param> /// <returns></returns> public static T DeserializeStringToObject <T>(string serializedGraph, SerializationFormatterType formatterType) { Byte[] arrGraph = Convert.FromBase64String(serializedGraph); using (MemoryStream memoryStream = new MemoryStream(arrGraph)) { IRemotingFormatter formatter = GetFormatter(formatterType); return((T)formatter.Deserialize(memoryStream)); } }
/// <summary> /// 序列化对象 /// </summary> /// <param name="graph"></param> /// <param name="formatterType"></param> /// <returns></returns> public static string SerializeObjectToString(object graph, SerializationFormatterType formatterType) { using (MemoryStream memoryStream = new MemoryStream()) { IRemotingFormatter formatter = GetFormatter(formatterType); formatter.Serialize(memoryStream, graph); Byte[] arrGraph = memoryStream.ToArray(); return(Convert.ToBase64String(arrGraph)); } }
private static IRemotingFormatter GetFormatter(SerializationFormatterType formatterType) { switch (formatterType) { case SerializationFormatterType.Binary: return(new BinaryFormatter()); default: throw new NotSupportedException(); } }
/// <summary> /// 把对象序列化转换为字符串 /// </summary> /// <param name="graph">可串行化对象实例</param> /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param> /// <returns>串行化转化结果</returns> /// <remarks>调用BinaryFormatter或SoapFormatter的Serialize方法实现主要转换过程。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\SerializationHelperTest.cs" region="TestSerializeAndDeserialize" lang="cs" title="把对象转换为字符串" /> /// </remarks> public static string SerializeObjectToString(this object graph, SerializationFormatterType formatterType) { ExceptionHelper.FalseThrow <ArgumentNullException>(graph != null, "graph"); using (MemoryStream memoryStream = new MemoryStream()) { IRemotingFormatter formatter = GetFormatter(formatterType); formatter.Serialize(memoryStream, graph); Byte[] arrGraph = memoryStream.ToArray(); return(Convert.ToBase64String(arrGraph)); } }
/// <summary> /// 把已序列化为字符串类型的对象反序列化为指定的类型 /// </summary> /// <param name="serializedGraph">已序列化为字符串类型的对象</param> /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param> /// <param name="binder"></param> /// <returns>串行化转化结果</returns> /// <remarks>调用BinaryFormatter或SoapFormatter的Deserialize方法实现主要转换过程。</remarks> public static object DeserializeStringToObject(this string serializedGraph, SerializationFormatterType formatterType, SerializationBinder binder) { ExceptionHelper.CheckStringIsNullOrEmpty(serializedGraph, "serializedGraph"); Byte[] arrGraph = Convert.FromBase64String(serializedGraph); using (MemoryStream memoryStream = new MemoryStream(arrGraph)) { IRemotingFormatter formatter = GetFormatter(formatterType); formatter.Binder = binder; return(formatter.Deserialize(memoryStream)); } }
/// <summary> /// /// </summary> /// <param name="formatterType"></param> /// <param name="binder"></param> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> private static IFormatter GetFormatter(SerializationFormatterType formatterType, SerializationBinder binder) { switch (formatterType) { case SerializationFormatterType.Binary: return(new BinaryFormatter() { Binder = binder }); #if IS_FRAMEWORK case SerializationFormatterType.Soap: return(new SoapFormatter()); #endif default: throw new NotSupportedException($"{nameof(formatterType)}为不支持的序列化类型"); } }
public static bool SerializeObjectToFile(object graph, SerializationFormatterType formatterType, string path) { if (graph == null) { throw new ArgumentNullException("graph"); } if (SerializationFormatterType.Xml == formatterType) { return(XmlSerializeObjectToFile(graph, path)); } using (FileStream fileStream = new FileStream(path, FileMode.Create)) { IRemotingFormatter formatter = GetFormatter(formatterType); formatter.Serialize(fileStream, graph); fileStream.Flush(); return(true); } }
public static string SerializeObject(object graph, SerializationFormatterType formatterType) { if (graph == null) { throw new ArgumentNullException("graph"); } if (SerializationFormatterType.Xml == formatterType) { return(XmlSerializeObject(graph)); } using (MemoryStream memoryStream = new MemoryStream()) { IRemotingFormatter formatter = GetFormatter(formatterType); formatter.Serialize(memoryStream, graph); byte[] arrGraph = memoryStream.ToArray(); return(Convert.ToBase64String(arrGraph)); } }
public static T DeserializeObjectFromFile <T>(string path, SerializationFormatterType formatterType, SerializationBinder binder = null) { if (!File.Exists(path)) { throw new FileNotFoundException("path"); } if (SerializationFormatterType.Xml == formatterType) { return(XmlDeserializeObjectFromFile <T>(path)); } using (FileStream fileStream = new FileStream(path, FileMode.Open)) { IRemotingFormatter formatter = GetFormatter(formatterType); if (binder != null) { formatter.Binder = binder; } return((T)formatter.Deserialize(fileStream)); } }
public static T DeserializeObject <T>(string serializedGraph, SerializationFormatterType formatterType, SerializationBinder binder = null) { if (string.IsNullOrEmpty(serializedGraph.Trim())) { throw new ArgumentNullException("serializedGraph"); } if (SerializationFormatterType.Xml == formatterType) { return(XmlDeserializeObject <T>(serializedGraph)); } byte[] arrGraph = Convert.FromBase64String(serializedGraph); using (MemoryStream memoryStream = new MemoryStream(arrGraph)) { IRemotingFormatter formatter = GetFormatter(formatterType); if (binder != null) { formatter.Binder = binder; } return((T)formatter.Deserialize(memoryStream)); } }
/// <summary> /// 把已序列化为字符串类型的对象反序列化为指定的类型 /// </summary> /// <param name="serializedGraph">已序列化为字符串类型的对象</param> /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param> /// <returns>串行化转化结果</returns> /// <remarks>调用BinaryFormatter或SoapFormatter的Deserialize方法实现主要转换过程。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\SerializationHelperTest.cs" region="TestSerializeAndDeserialize" lang="cs" title="把字符串对象转换为指定的类型" /> /// </remarks> public static object DeserializeStringToObject(this string serializedGraph, SerializationFormatterType formatterType) { return(DeserializeStringToObject(serializedGraph, formatterType, null)); }
/// <summary> /// 把已序列化为字符串类型的对象反序列化为指定的类型 /// </summary> /// <typeparam name="T">对象转换后的类型</typeparam> /// <param name="serializedGraph">已序列化为字符串类型的对象</param> /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param> /// <param name="binder">反序列化时的类型转换器</param> /// <returns>串行化转化结</returns> /// <remarks>调用BinaryFormatter或SoapFormatter的Deserialize方法实现主要转换过程。</remarks> public static T DeserializeStringToObject <T>(this string serializedGraph, SerializationFormatterType formatterType, SerializationBinder binder) { return((T)DeserializeStringToObject(serializedGraph, formatterType, binder)); }
/// <summary> /// 将对象实例序列化到文件里 /// </summary> /// <param name="graph">序列化对象</param> /// <param name="formatterType">序列化格式编码</param> /// <param name="path">序列化文件路径</param> /// <param name="binder"></param> /// <exception cref="ArgumentNullException"><paramref name="graph"/>为null</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static bool SerializeObjectToFile(object graph, SerializationFormatterType formatterType, string path, SerializationBinder binder = null)
/// <summary> /// 将对象实例序列化成字符串 /// </summary> /// <param name="graph">序列化对象</param> /// <param name="formatterType">序列化格式编码</param> /// <param name="binder"></param> /// <exception cref="ArgumentNullException"><paramref name="graph"/>为null</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static string SerializeObject(object graph, SerializationFormatterType formatterType, SerializationBinder binder = null)
/// <summary> /// 将文件内容反序列化成对象实例 /// </summary> /// <typeparam name="T">反序列化类型</typeparam> /// <param name="path">反序列化文件路径</param> /// <param name="formatterType">序列化格式编码</param> /// <param name="binder"></param> /// <exception cref="FileNotFoundException"><paramref name="path"/>指定的文件不存在</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static T DeserializeObjectFromFile <T>(string path, SerializationFormatterType formatterType, SerializationBinder binder = null)
/// <summary> /// 将字符串反序列化成对象实例 /// </summary> /// <typeparam name="T">反序列化类型</typeparam> /// <param name="serializedGraph">反序列化字符串</param> /// <param name="formatterType">序列化格式编码</param> /// <param name="binder"></param> /// <exception cref="ArgumentException"><paramref name="serializedGraph"/>为null或空白字符串</exception> /// <exception cref="NotSupportedException"><paramref name="formatterType"/>为不支持的序列化类型</exception> /// <returns></returns> public static T DeserializeObject <T>(string serializedGraph, SerializationFormatterType formatterType, SerializationBinder binder = null)