C#序列化与反序列化以及深拷贝浅拷贝
时间: 2023-07-08 14:07:08 浏览: 111
C#的序列化和反序列化是将对象转换为二进制数据或将二进制数据转换为对象的过程。在C#中,可以使用BinaryFormatter、XmlSerializer、DataContractSerializer等多种方式进行序列化和反序列化。
深拷贝和浅拷贝用于复制对象。浅拷贝只复制对象的引用,而不会复制对象的实际内容。深拷贝则会完全复制对象的内容,包括引用类型的成员变量。
在C#中,可以通过实现ICloneable接口来实现浅拷贝。对于深拷贝,则需要自己手动实现一个递归复制对象的方法。
下面是一个示例代码,演示了如何进行序列化、反序列化以及深拷贝和浅拷贝:
```csharp
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
[Serializable]
public class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
public object Clone()
{
return new Person
{
Name = Name,
Age = Age,
Address = (Address)Address?.Clone()
};
}
}
[Serializable]
public class Address : ICloneable
{
public string City { get; set; }
public string Street { get; set; }
public object Clone()
{
return new Address
{
City = City,
Street = Street
};
}
}
public static class SerializationHelper
{
public static byte[] SerializeToBinary(object obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.ToArray();
}
}
public static T DeserializeFromBinary<T>(byte[] data)
{
using (var ms = new MemoryStream(data))
{
var formatter = new BinaryFormatter();
return (T)formatter.Deserialize(ms);
}
}
public static string SerializeToXml(object obj)
{
using (var writer = new StringWriter())
{
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(writer, obj);
return writer.ToString();
}
}
public static T DeserializeFromXml<T>(string xml)
{
using (var reader = new StringReader(xml))
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(reader);
}
}
}
public static class ObjectHelper
{
public static T ShallowCopy<T>(this T obj)
{
return (T)obj.MemberwiseClone();
}
public static T DeepCopy<T>(this T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
}
// 示例代码
var person1 = new Person
{
Name = "Tom",
Age = 30,
Address = new Address
{
City = "Beijing",
Street = "Chang'an Avenue"
}
};
// 深拷贝
var person2 = person1.DeepCopy();
person2.Name = "Jerry";
person2.Address.City = "Shanghai";
Console.WriteLine($"person1.Name={person1.Name}, person1.Address.City={person1.Address.City}");
Console.WriteLine($"person2.Name={person2.Name}, person2.Address.City={person2.Address.City}");
// 浅拷贝
var person3 = person1.ShallowCopy();
person3.Name = "John";
person3.Address.City = "Guangzhou";
Console.WriteLine($"person1.Name={person1.Name}, person1.Address.City={person1.Address.City}");
Console.WriteLine($"person3.Name={person3.Name}, person3.Address.City={person3.Address.City}");
// 序列化和反序列化
var data = SerializationHelper.SerializeToBinary(person1);
var person4 = SerializationHelper.DeserializeFromBinary<Person>(data);
Console.WriteLine($"person1.Name={person1.Name}, person1.Address.City={person1.Address.City}");
Console.WriteLine($"person4.Name={person4.Name}, person4.Address.City={person4.Address.City}");
var xml = SerializationHelper.SerializeToXml(person1);
var person5 = SerializationHelper.DeserializeFromXml<Person>(xml);
Console.WriteLine($"person1.Name={person1.Name}, person1.Address.City={person1.Address.City}");
Console.WriteLine($"person5.Name={person5.Name}, person5.Address.City={person5.Address.City}");
```
输出结果:
```
person1.Name=Tom, person1.Address.City=Beijing
person2.Name=Jerry, person2.Address.City=Shanghai
person1.Name=Tom, person1.Address.City=Guangzhou
person3.Name=John, person3.Address.City=Guangzhou
person1.Name=Tom, person1.Address.City=Beijing
person4.Name=Tom, person4.Address.City=Beijing
person1.Name=Tom, person1.Address.City=Beijing
person5.Name=Tom, person5.Address.City=Beijing
```
阅读全文