This commit is contained in:
2026-02-23 19:18:51 +08:00
parent f7f4c94c00
commit 0fba392cb0
19 changed files with 67 additions and 56 deletions

View File

@@ -8,13 +8,11 @@ using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Linq;
namespace iMES.Core.Extensions
{
@@ -543,23 +541,28 @@ namespace iMES.Core.Extensions
return info;
}
public static T FromBytes<T>(this byte[] data)
public static byte[] ToBytes(this object obj)
{
if (data == null || data.Length == 0)
if (obj == null)
return null;
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
return default(T);
bf.Serialize(ms, obj);
return ms.ToArray();
}
return JsonSerializer.Deserialize<T>(data);
}
public static T ToObject<T>(this byte[] source)
public static object ToObject(this byte[] source)
{
if (source == null || source.Length == 0)
using (var memStream = new MemoryStream())
{
return default(T);
var bf = new BinaryFormatter();
memStream.Write(source, 0, source.Length);
memStream.Seek(0, SeekOrigin.Begin);
var obj = bf.Deserialize(memStream);
return obj;
}
return JsonSerializer.Deserialize<T>(source);
}
/// <summary>