better-genshin-impact/BetterGenshinImpact/Helpers/ObjectUtils.cs

37 lines
963 B
C#
Raw Permalink Normal View History

2024-07-01 00:22:36 +08:00
using System;
using System.IO;
2024-04-21 17:18:03 +08:00
using System.Runtime.Serialization.Formatters.Binary;
namespace BetterGenshinImpact.Helpers;
public class ObjectUtils
{
2024-07-01 00:22:36 +08:00
[Obsolete("Obsolete")]
2024-04-21 17:18:03 +08:00
public static byte[] Serialize(object obj)
{
var ms = new MemoryStream();
var formatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
formatter.Serialize(ms, obj);
#pragma warning restore SYSLIB0011
byte[] bytes = ms.GetBuffer();
return bytes;
}
2024-07-01 00:22:36 +08:00
[Obsolete("Obsolete")]
2024-04-21 17:18:03 +08:00
public static object Deserialize(byte[] bytes)
{
//利用传来的byte[]创建一个内存流
var ms = new MemoryStream(bytes)
{
Position = 0
};
var formatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
var obj = formatter.Deserialize(ms); //把内存流反序列成对象
#pragma warning restore SYSLIB0011
ms.Close();
return obj;
}
}