125 lines
4.7 KiB
C#
Executable File
125 lines
4.7 KiB
C#
Executable File
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared
|
|
{
|
|
public static class ObjectExtensions
|
|
{
|
|
public static object ObjectCopyTo(this object obj)
|
|
{
|
|
Object targetDeepCopyObj;
|
|
Type targetType = obj.GetType();
|
|
//值类型
|
|
if (targetType.IsValueType == true)
|
|
{
|
|
targetDeepCopyObj = obj;
|
|
}
|
|
//引用类型
|
|
else
|
|
{
|
|
targetDeepCopyObj = Activator.CreateInstance(targetType); //创建引用对象
|
|
MemberInfo[] memberCollection = obj.GetType().GetMembers();
|
|
|
|
foreach (MemberInfo member in memberCollection)
|
|
{
|
|
if (member.MemberType == MemberTypes.Field)
|
|
{
|
|
FieldInfo field = (FieldInfo)member;
|
|
Object fieldValue = field.GetValue(obj);
|
|
if (fieldValue is ICloneable)
|
|
{
|
|
field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone());
|
|
}
|
|
else
|
|
{
|
|
field.SetValue(targetDeepCopyObj, ObjectCopyTo(fieldValue));
|
|
}
|
|
|
|
}
|
|
else if (member.MemberType == MemberTypes.Property)
|
|
{
|
|
PropertyInfo myProperty = (PropertyInfo)member;
|
|
MethodInfo info = myProperty.GetSetMethod(false);
|
|
if (info != null)
|
|
{
|
|
object propertyValue = myProperty.GetValue(obj, null);
|
|
if (propertyValue is ICloneable)
|
|
{
|
|
myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);
|
|
}
|
|
else
|
|
{
|
|
myProperty.SetValue(targetDeepCopyObj, ObjectCopyTo(propertyValue), null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return targetDeepCopyObj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实现对象的深拷贝
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static T CreateDeepCopy<T>(T obj)
|
|
{
|
|
#region 二进制序列化的方式进行深拷贝
|
|
//T t;
|
|
//MemoryStream memoryStream = new MemoryStream();
|
|
//BinaryFormatter formatter = new BinaryFormatter();
|
|
//formatter.Serialize(memoryStream, obj);
|
|
//memoryStream.Position = 0;
|
|
//t = (T)formatter.Deserialize(memoryStream);
|
|
//return t;
|
|
#endregion
|
|
|
|
#region xml序列化的方式实现深拷贝
|
|
XmlSerializer xml = new XmlSerializer(typeof(T));
|
|
using (MemoryStream ms = new MemoryStream())//创建内存流
|
|
{
|
|
//将对象序列化到内存中
|
|
xml.Serialize(ms, obj);
|
|
ms.Position = default;//将内存流的位置设为0
|
|
return (T)xml.Deserialize(ms);//继续反序列化
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实现对象的深拷贝
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static T DeepCopy<T>(this T obj)
|
|
{
|
|
#region 二进制序列化的方式进行深拷贝
|
|
//T t;
|
|
//MemoryStream memoryStream = new MemoryStream();
|
|
//BinaryFormatter formatter = new BinaryFormatter();
|
|
//formatter.Serialize(memoryStream, obj);
|
|
//memoryStream.Position = 0;
|
|
//t = (T)formatter.Deserialize(memoryStream);
|
|
//return t;
|
|
#endregion
|
|
|
|
#region xml序列化的方式实现深拷贝
|
|
XmlSerializer xml = new XmlSerializer(typeof(T));
|
|
using (MemoryStream ms = new MemoryStream())//创建内存流
|
|
{
|
|
//将对象序列化到内存中
|
|
xml.Serialize(ms, obj);
|
|
ms.Position = default;//将内存流的位置设为0
|
|
return (T)xml.Deserialize(ms);//继续反序列化
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
}
|