日期:2014-05-17 浏览次数:20946 次
/// <summary>
/// 通过对象的序列化实现对象的深拷贝
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="RealObject">原始对象</param>
/// <returns>原始对象的深拷贝</returns>
public static T CloneObj<T>(T RealObject)
where T : BaseElement
{
using (Stream objectStream = new MemoryStream())
{
//利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, RealObject);
objectStream.Seek(0, SeekOrigin.Begin);
return (formatter.Deserialize(objectStream) as T);
}
}