码迷,mamicode.com
首页 > Windows程序 > 详细

C# Clone控件

时间:2014-11-24 11:47:12      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   sp   for   on   

    /// <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控件

标签:des   style   blog   io   ar   color   sp   for   on   

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

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