| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Abp.Logging;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace VberZero.Tools.StringModel;
- public static class SerializeHelper
- {
- /// <summary>
- /// 深拷贝(对象需有[Serializable]可序列化特性)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="target"></param>
- /// <returns></returns>
- public static T DeepClone<T>(this T target)
- {
- return VzDerializable<T>(VzSerializable(target));
- }
- public static string VzSerializable(object target)
- {
- try
- {
- using (MemoryStream stream = new MemoryStream())
- {
- new BinaryFormatter().Serialize(stream, target);
- return Convert.ToBase64String(stream.ToArray());
- }
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- return null;
- }
- }
- public static T VzDerializable<T>(string target)
- {
- try
- {
- byte[] targetArray = Convert.FromBase64String(target);
- using (MemoryStream stream = new MemoryStream(targetArray))
- {
- return (T)new BinaryFormatter().Deserialize(stream);
- }
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- return default(T);
- }
- }
- }
|