结合微软的部分代码再加上自己整理的代码实现的一个我个人觉得很强大的序列化/反序化类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Xml.Serialization;
  5 using System.IO;
  6 using System.Runtime.Serialization.Formatters.Soap;
  7 using System.Runtime.Serialization.Formatters.Binary;
  8 using System.Runtime.Remoting.Messaging;
  9 using System.Runtime.Serialization;
 10 
 11 namespace eBriny.Framework.Core
 12 {
 13     /// <summary>
 14     /// 枚举类型,其包括Xml、Soap和Binary串行化编码方式
 15     /// </summary>
 16     public enum SerializationFormatterType
 17     {
 18         /// <summary>
 19         /// Xml消息格式编码
 20         /// </summary>
 21         Xml,
 22         /// <summary>
 23         /// SOAP消息格式编码
 24         /// </summary>
 25         Soap,
 26         /// <summary>
 27         /// 二进制消息格式编码
 28         /// </summary>
 29         Binary
 30     }
 31     /// <summary>
 32     /// 帮助对象实现序列化和反序列化
 33     /// </summary>
 34     /// <remarks>
 35     /// 对象序列化是把对象序列化转化为string类型,对象反序列化是把对象从string类型反序列化转化为其源类型。
 36     /// </remarks>
 37     public static class SerializationHelper
 38     {
 39         /// <summary>
 40         /// 按照串行化的编码要求,生成对应的编码器。
 41         /// </summary>
 42         /// <param name="formatterType"></param>
 43         /// <returns></returns>
 44         private static IRemotingFormatter GetFormatter(SerializationFormatterType formatterType)
 45         {
 46             switch (formatterType)
 47             {
 48                 case SerializationFormatterType.Binary:
 49                     return new BinaryFormatter();
 50                 case SerializationFormatterType.Soap:
 51                     return new SoapFormatter();
 52                 case SerializationFormatterType.Xml:
 53                 default:
 54                     throw new NotSupportedException();
 55             }
 56         }
 57         /// <summary>
 58         /// 把对象序列化为字符串
 59         /// </summary>
 60         /// <param name="graph">可串行化对象实例</param>
 61         /// <param name="formatterType">消息格式编码类型(Xml、Soap或Binary)</param>
 62         /// <returns>串行转化结果</returns>
 63         public static string SerializeObject(object graph, SerializationFormatterType formatterType)
 64         {
 65             if (graph == null)
 66             {
 67                 throw new ArgumentNullException("graph");
 68             }
 69             if (SerializationFormatterType.Xml == formatterType)
 70             {
 71                 return SerializeObject(graph);
 72             }
 73             else
 74             {
 75                 using (MemoryStream memoryStream = new MemoryStream())
 76                 {
 77                     IRemotingFormatter formatter = GetFormatter(formatterType);
 78                     formatter.Serialize(memoryStream, graph);
 79                     Byte[] arrGraph = memoryStream.ToArray();
 80                     return Convert.ToBase64String(arrGraph);
 81                 }
 82             }
 83         }
 84         /// <summary>
 85         /// 把已序列化为字符串类型的对象反序列化为指定的类型
 86         /// </summary>
 87         /// <typeparam name="T"></typeparam>
 88         /// <param name="serializedGraph">已序列化为字符串类型的对象</param>
 89         /// <param name="formatterType">消息格式编码类型(Xml、Soap或Binary)</param>
 90         /// <param name="binder">如果消息格式编码类型为Xml则此项请输入Null</param>
 91         /// <returns>串行转化结果</returns>
 92         public static T DeserializeObject<T>(string serializedGraph, SerializationFormatterType formatterType, SerializationBinder binder)
 93         {
 94             if (string.IsNullOrEmpty(serializedGraph.Trim()))
 95             {
 96                 throw new ArgumentNullException("serializedGraph");
 97             }
 98             if (SerializationFormatterType.Xml == formatterType)
 99             {
100                 return DeserializeObject<T>(serializedGraph);
101             }
102             else
103             {
104                 Byte[] arrGraph = Convert.FromBase64String(serializedGraph);
105 
106                 using (MemoryStream memoryStream = new MemoryStream(arrGraph))
107                 {
108                     IRemotingFormatter formatter = GetFormatter(formatterType);
109 
110                     formatter.Binder = binder;
111 
112                     return (T)formatter.Deserialize(memoryStream);
113                 }
114             }
115         }
116         private static T DeserializeObject<T>(string serializedGraph)
117         {
118             XmlSerializer xs = new XmlSerializer(typeof(T));
119             StreamReader sr = new StreamReader(serializedGraph);
120             try
121             {
122                 return (T)xs.Deserialize(sr);
123             }
124             catch (Exception err)
125             {
126                 throw err;
127             }
128             finally
129             {
130                 if (sr != null)
131                 {
132                     sr.Close(); sr.Dispose();
133                 }
134             }
135         }
136 
137         private static string SerializeObject(object graph)
138         {
139             XmlSerializer xs = new XmlSerializer(graph.GetType());
140             StringWriter sw = new StringWriter();
141             try
142             {
143                 xs.Serialize(sw, graph);
144                 sw.Flush();
145                 return sw.ToString();
146             }
147             catch (Exception err)
148             {
149                 throw err;
150             }
151             finally
152             {
153                 if (sw != null)
154                 {
155                     sw.Close(); sw.Dispose();
156                 }
157             }
158         }
159 
160         /// <summary>
161         /// 通过序列化复制对象
162         /// </summary>
163         /// <param name="graph"></param>
164         /// <returns></returns>
165         public static object CloneObject(object graph)
166         {
167             if (string.IsNullOrEmpty(graph.ToString()))
168             {
169                 throw new ArgumentNullException("graph");
170             }
171             using (MemoryStream memoryStream = new MemoryStream(1024))
172             {
173                 BinaryFormatter formatter = new BinaryFormatter();
174 
175                 formatter.Serialize(memoryStream, graph);
176 
177                 memoryStream.Position = 0;
178 
179                 return formatter.Deserialize(memoryStream);
180             }
181         }
182     }
183 }
posted @ 2012-07-02 17:35  star丶清影  阅读(622)  评论(0编辑  收藏  举报