using System;
namespace Sp.Common
{
public class FunctionHelper
{
/// <summary>
/// 深度复制
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(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);
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.Stream stream = new System.IO.MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, System.IO.SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
}