using System.IO;
using System.Runtime.Serialization;
namespace DeepCopyExp
{
class DeepCopy
{
public static T DeepCopyByDCS<T>(T obj)
{
T newObject;
using (MemoryStream memoryStream = new MemoryStream())
{
DataContractSerializer dcs = new DataContractSerializer(obj.GetType());
dcs.WriteObject(memoryStream, obj);
memoryStream.Seek(0, SeekOrigin.Begin);
newObject = (T)dcs.ReadObject(memoryStream);
}
return newObject;
}
}
}
using System;
namespace DeepCopyExp
{
class Program
{
static void Main(string[] args)
{
Student s = new Student()
{
Id = 1,
Name = "三五月儿",
Score = new Score() { ChineseScore =100, MathScore=100}
};
Student s1 = DeepCopy.DeepCopyByDCS(s);
Console.WriteLine("Id = {0}, Name = {1}, Score.ChineseScore = {2}, Score.MathScore = {3}", s1.Id, s1.Name, s1.Score.ChineseScore, s1.Score.MathScore);
}
}
public class Score
{
public int ChineseScore { get; set; }
public int MathScore { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Score Score { get; set; }
}
}
s.Id = 2;
s.Name = "sanwuyueer";
s.Score = new Score() { ChineseScore = 0, MathScore = 0 };
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有