C# Clone控件

时间:2023-03-09 07:42:30
C# Clone控件
    /// <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(, SeekOrigin.Begin);
return (T)formatter.Deserialize(objectStream);
}
}