58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
|
|
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using MongoDB.Bson.IO;
|
|
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Bson.Serialization.Serializers;
|
|
|
|
namespace ET
|
|
{
|
|
public static class DeepCloneHelper
|
|
{
|
|
/// <summary>
|
|
/// 深拷贝
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static T DeepCopy<T>(this T obj)
|
|
where T : class
|
|
{
|
|
try
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return null;
|
|
}
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
BsonSerializer.Serialize(new BsonBinaryWriter(stream), obj);
|
|
stream.Position = 0;
|
|
return BsonSerializer.Deserialize<T>(stream);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static T DeepCopyByReflect<T>(T obj)
|
|
{
|
|
//如果是字符串或值类型则直接返回
|
|
if (obj is string || obj.GetType().IsValueType) return obj;
|
|
|
|
object retval = Activator.CreateInstance(obj.GetType());
|
|
FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
|
|
foreach (FieldInfo field in fields)
|
|
{
|
|
try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); }
|
|
catch { }
|
|
}
|
|
return (T)retval;
|
|
}
|
|
}
|
|
} |