深拷贝:指的是拷贝一个对象时,不仅仅把对象的引用进行复制,还把该对象引用的值也一起拷贝。
浅拷贝:指的是拷贝一个对象时,仅仅拷贝对象的引用进行拷贝,但是拷贝对象和源对象还是引用同一份实体。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace Pratise222
{
class DeepCopy
{
static void Main(string[] arg)
{
#region 深拷贝与浅拷贝
//People llb = new People()
//{
// Name = "llb",
// Age = 24,
// Email = "[email protected]"
//};
没有发生任何对象拷贝
//People zs = llb;
//Console.WriteLine("ok");
//People llb = new People()
//{
// Name = "llb",
// Age = 24,
// Email = "[email protected]",
// bike=new Bike(){Brand="永久"}
//};
下面是浅拷贝
//People lll = new People();
//lll.Name = llb.Name;
//lll.Age = llb.Age;
//lll.Email = llb.Email;
//lll.bike = llb.bike;
此时lll与llb是两个不同的对象。在内存中有两个对象内存,所以此时已经发生了对象的拷贝
深拷贝
//People pl = new People();
//pl.Name = "hello";
//pl.Age = 21;
//pl.Email = "[email protected]";
//pl.bike = new Bike() { Brand = "凤凰" };
//People p2 = new People();
//p2.Name = pl.Name;
//p2.Age = pl.Age;
//p2.Email = pl.Email;
//p2.bike = new Bike() { Brand = pl.bike.Brand };
//Console.ReadKey();
#endregion
#region 实现深拷贝与浅拷贝
//1.实现浅拷贝
People p1 = new People();
p1.Name = "hello";
p1.Age = 19;
p1.Email = "[email protected]";
p1.bike = new Bike() { Brand="ddddf"};
People p2 = p1.SwallowCopy();
Console.WriteLine("浅拷贝完成");
Console.ReadKey();
People p3 = p1.DeepCopy();
Console.WriteLine("浅拷贝完成");
//2.实现深拷贝 通过二进制序列化实现
#endregion
}
}
public class People
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
public Bike bike
{
get;
set;
}
public People SwallowCopy()
{
//一句话实现浅拷贝
return this.MemberwiseClone() as People;
//MemberwiseClone()的作用就是吧当前对象浅拷贝一份
}
public People DeepCopy()
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms=new MemoryStream())
{
bf.Serialize(ms,this);
//紧接着执行反序列化
ms.Position = 0;
return bf.Deserialize(ms) as People;
}
}
}
public class Bike
{
public string Brand
{
get;
set;
}
}
}
二、序列化到XML
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace Pratise222
{
class XmlSer
{
//static void Main(string[] args)
//{
// #region Myregion
// //List
// //list.Add(new Person(){Name="张三",Age=18,Email="ddff"});
// //list.Add(new Person() { Name = "李四", Age = 28, Email = "dcgf" });
// //list.Add(new Person() { Name = "虚妄里", Age = 19, Email = "dgg" });
// //list.Add(new Person() { Name = "hello", Age = 78, Email = "d99" });
// 实现xml序列化
// //XmlSerializer xml = new XmlSerializer(typeof(List
// //using (FileStream fsWrite = File.OpenWrite("list.xml"))
// //{
// // xml.Serialize(fsWrite, list);
// //}
// //Console.WriteLine("ok");
// //Console.ReadKey();
// #endregion
// #region 自己序列化
// //List
// //list.Add(new Person() { Name = "张三", Age = 18, Email = "ddff" });
// //list.Add(new Person() { Name = "李四", Age = 28, Email = "dcgf" });
// //list.Add(new Person() { Name = "虚妄里", Age = 19, Email = "dgg" });
// //list.Add(new Person() { Name = "hello", Age = 78, Email = "d99" });
// //MySerialize(list,typeof(List
// //Console.WriteLine("ok");
// //Person p = new Person() { Name = "fuck", Age = 18, Email = "[email protected]" };
// //MySerialize(p, typeof(Person));
// // Car car = new Car()
// // {
// // Brand = "auto",
// // Number = "A#R#RF##666",
// // PP = "9t"
// // };
// // XmlSerializer xml=new XmlSerializer(typeof(Car));
// // using (FileStream fs = File.OpenWrite("Mycar.xml"))
// // {
// // xml.Serialize(fs, car);
// // }
// MySerialize(car, typeof(Car));
// // Console.WriteLine("ok");
// // Console.ReadKey();
// #endregion
// Car car = new Car()
// {
// Brand = "auto",
// Number = "A#R#RF##666",
// PP = "9t"
// };
// MySerialize(car, typeof(Car));
//}
private static void MySerialize(object obj, Type type)
{
Type ilistType = typeof(IList
//1.判断当前这个类型是否实现了IList
if (ilistType.IsAssignableFrom(type))
{
//按照集合的方式来序列化
}
else
{
//按照对象的方式来序列化
//把当前对象写入到xml文件中
//写入xml文件,把类名作为根节点
XDocument document = new XDocument();
//写入根节点
string nsstr = type.ToString();
string classname = nsstr.Substring(nsstr.LastIndexOf('.') + 1);
XElement rootElement = new XElement(classname);
//获取当前类型的所有属性
PropertyInfo[] properties = type.GetProperties();
//遍历当前类型中的每个属性
foreach (PropertyInfo item in properties)
{
//现在该如何获取这个属性是否被标记了MyXmlIgnore标记呢?
object[] objsAttr= item.GetCustomAttributes(typeof(MyXmlIgnoreAttribute),false);
if (objsAttr.Length > 0)
{
continue;
}
//将当前属性写到xml文件中(其实就是序列化到了该文件中)
rootElement.SetElementValue(item.Name, item.GetValue(obj,null));
}
document.Add(rootElement);
document.Save(classname+".xml");
}
//2.如果实现了该接口则把当前对象当做集合来进行序列话
//3.如果没有实现该接口,则把当前对象当做一个普通的对象来序列化。
}
}
public class Person
{
public Person()
{
}
public Person(string name, int age, string email)
{
this.Name = name;
this.Age = age;
this.Email = email;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
}
public class Car
{
[XmlIgnore]//不被序列化 xml中
// [NonSerialized] //普通类
public string Brand
{
get;
set;
}
[MyXmlIgnore]
public string Number
{
get;
set;
}
public string PP
{
get;
set;
}
}
public class MyXmlIgnoreAttribute : Attribute
{
}
}