using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace jquery
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int i = 10;
i=DeepCloneObject<int>.DeepClone(i);
}
}
/// <summary>
/// 实现深拷贝
/// </summary>
/// <typeparam name="T">类型</typeparam>
public static class DeepCloneObject<T>
{
public static T DeepClone(T param)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, param);
memoryStream.Position = 0;
return (T)formatter.Deserialize(memoryStream);
}
}
}
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |