You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.4 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace JwKdsV.Core.Utils
{
public static class ObjectUtils
{
//要复制的实例必须可序列化,包括实例引用的其它实例都必须在类定义时加[Serializable]特性。
public static T Copy<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable." , "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source , null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream , source);
stream.Seek(0 , SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
public static void CopyTo(this object S , object T)
{
foreach (var pS in S.GetType().GetProperties())
{
foreach (var pT in T.GetType().GetProperties())
{
if (pT.Name != pS.Name) continue;
(pT.GetSetMethod()).Invoke(T , new object[]{ pS.GetGetMethod().Invoke( S, null ) });
}
};
}
public static T CloneJson<T>(this T source)
{
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source , null))
{
return default(T);
}
// initialize inner objects individually
// for example in default constructor some list property initialized with some values,
// but in 'source' these items are cleaned -
// without ObjectCreationHandling.Replace default constructor values will be added to result
var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source) , deserializeSettings);
}
}
}