码迷,mamicode.com
首页 > 其他好文 > 详细

CloneObject

时间:2017-05-31 16:45:39      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:origin   ref   begin   sse   throw   bsp   formatter   als   type   

 public static T Clone<T>(this T source)
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            // Don‘t serialize a null object, simply return the default for that object
            if (Object.ReferenceEquals(source, null))
            {
                return default(T);
            }

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new MemoryStream();
            using (stream)
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(stream);
            }
        }

 

CloneObject

标签:origin   ref   begin   sse   throw   bsp   formatter   als   type   

原文地址:http://www.cnblogs.com/zhshlimi/p/6924584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!