zoukankan      html  css  js  c++  java
  • C#的对象复制

    用序列化和反序列化的方法来实现对对象的深拷贝。

    public static T DeepCopy<t>(T obj)
    {
        object retval;
        using (MemoryStream ms = new MemoryStream())
        {
            BinaryFormatter bf = new BinaryFormatter();
            //序列化成流
            bf.Serialize(ms, obj);
            ms.Seek(0, SeekOrigin.Begin);
            //反序列化成对象
            retval = bf.Deserialize(ms);
            ms.Close();
        }
        return (T)retval;
    }

    通过序列化为 Base64String 的形式还复制:对于包含图片等Bit字节之类的实体,直接通过上方法复制出来的实体结果不正确(知道原因的朋友敬请评论),可以通过转化为Base64String的形式,就可以得到正确的结果,对保存到数据库也比较方便。

            /// <summary>
            /// 复制控件。
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="serializeObj"></param>
            /// <returns></returns>
            public static T GetObjectClone<T>(T serializeObj)
            {
                string base64String = SerializeObject<T>(serializeObj);
                return DeserializeObject<T>(base64String);
            }

            /// <summary>
            /// 序列化为Base64String。
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="serializeObj"></param>
            /// <returns></returns>
            public static string SerializeObject<T>(T serializeObj)
            {
                string base64String = string.Empty;
                MemoryStream ms = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                try
                {
                    formatter.Serialize(ms, serializeObj);
                    base64String = Convert.ToBase64String(ms.GetBuffer());
                }
                catch (SerializationException e)
                {
                    Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                    throw;
                }
                finally
                {
                    ms.Close();
                }
                return base64String;
            }

            /// <summary>
            /// 从Base64反序列化。
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="base64String"></param>
            /// <returns></returns>
            public static T DeserializeObject<T>(string base64String)
            {
                MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64String));
                BinaryFormatter formatter = new BinaryFormatter();

                try
                {
                    // Deserialize the hashtable from the file and
                    // assign the reference to the local variable.
                    return (T)formatter.Deserialize(ms);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                    throw;
                }
                finally
                {
                    ms.Close();
                }
            }

  • 相关阅读:
    jQuery插件 -- 表单验证插件jquery.validate.js
    jQuery插件 -- Form表单插件jquery.form.js<转>
    win7 64位安装oracle10g客户端心得
    用STS创建Maven的Web项目<转>
    分别通过【buildpath】和【lib】倒入JAR包有什么不同
    利用AbstractRoutingDataSource实现动态数据源切换
    mybatis分页
    Java基本功—Reference
    Java中 堆 栈,常量池等概念解析(转载)
    RTSP流媒体转发服务器源码
  • 原文地址:https://www.cnblogs.com/Yjianyong/p/1793066.html
Copyright © 2011-2022 走看看