C# 实现克隆的工具类型

来源:互联网 发布:金蝶数据库在哪里 编辑:程序博客网 时间:2024/06/04 18:21

  实现克隆的工具类型要求类型必须是可序列化的

public enum FormatterType
    {
        /// <summary>
        /// SOAP消息格式编码
        /// </summary>
        Soap,

        /// <summary>
        /// 二进制消息格式编码
        /// </summary>
        Binary
    }

    public static class SerializationHelper
    {
        private const FormatterType DefaultFormatterType = FormatterType.Binary;

        /// <summary>
        /// 按照串行化的编码要求,生成对应的编码器。
        /// </summary>
        /// <param name="formatterType"></param>
        /// <returns></returns>
        private static IRemotingFormatter GetFormatter(FormatterType formatterType)
        {
            switch (formatterType)
            {
                case FormatterType.Binary: return new BinaryFormatter();
                case FormatterType.Soap: return new SoapFormatter();
            }
            throw new NotSupportedException();
        }

        /// <summary>
        /// 把对象序列化转换为字符串
        /// </summary>
        /// <param name="graph">可串行化对象实例</param>
        /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param>
        /// <returns>串行化转化结果</returns>
        /// <remarks>调用BinaryFormatter或SoapFormatter的Serialize方法实现主要转换过程。
        /// </remarks>    
        public static string SerializeObjectToString(object graph, FormatterType formatterType)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                IRemotingFormatter formatter = GetFormatter(formatterType);
                formatter.Serialize(memoryStream, graph);
                Byte[] arrGraph = memoryStream.ToArray();
                return Convert.ToBase64String(arrGraph);
            }
        }
        public static string SerializeObjectToString(object graph)
        {
            return SerializeObjectToString(graph, DefaultFormatterType);
        }

        /// <summary>
        /// 把已序列化为字符串类型的对象反序列化为指定的类型
        /// </summary>
        /// <param name="serializedGraph">已序列化为字符串类型的对象</param>
        /// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param>
        /// <typeparam name="T">对象转换后的类型</typeparam>
        /// <returns>串行化转化结果</returns>
        /// <remarks>调用BinaryFormatter或SoapFormatter的Deserialize方法实现主要转换过程。
        /// </remarks>
        public static T DeserializeStringToObject<T>(string graph, FormatterType formatterType)
        {
            Byte[] arrGraph = Convert.FromBase64String(graph);
            using (MemoryStream memoryStream = new MemoryStream(arrGraph))
            {
                IRemotingFormatter formatter = GetFormatter(formatterType);
                return (T)formatter.Deserialize(memoryStream);
            }
        }

        public static T DeserializeStringToObject<T>(string graph)
        {
            return DeserializeStringToObject<T>(graph, DefaultFormatterType);
        }
    }

 

 

  public static class Test
    {
        public static T Clone<T>(this T t) where T : class
        {
            Type target = t.GetType();
            object[] attributes = target.GetCustomAttributes(typeof(SerializableAttribute), false);
            if (attributes.Length == 0)
            {
                //throw new ArgumentException("T must be Serializabled");采用json序列化
                //需要引用Newtonsoft.Json.dll
                Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();
                StringWriter sw = new StringWriter();
                Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(sw);
                ser.Serialize(writer, t);
                StringReader sr = new StringReader(sw.ToString());
                Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(sr);
                return ser.Deserialize<T>(reader);
            }
            else
            {
                string graph = SerializationHelper.SerializeObjectToString(t);
                return SerializationHelper.DeserializeStringToObject<T>(graph);
            }
        }

    }

需要引用System.Runtime.Serialization.Formatters.Soap.dll

原创粉丝点击