首页 > Windows开发 > 详细

C# Clone控件

时间:2014-11-24 11:46:02      阅读:313      评论:0      收藏:0      [点我收藏+]
    /// <summary>
    /// Perform a Clone of the object asdfas.
    /// </summary>
    /// <typeparam name="T">The type of object being cloned.</typeparam>
    /// <param name="source">The object instance to clone.</param>
    /// <returns>The cloned object.</returns>
    public static T Clone<T>(T RealObject)
    {
        if (!typeof(T).IsSerializable)
            throw new ArgumentException("The type must be marked Serializable", "RealObject");

        // No need to serialize (or clone) null object, simply return null (or the object itself)
        if (Object.ReferenceEquals(RealObject, null))
            return default(T);

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

 

C# Clone控件

原文:http://www.cnblogs.com/ilookbo/p/4118213.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!