Files
LL-28/tongxin/NetWorkHelper/Helper/SerHelper.cs
2025-11-14 09:41:37 +08:00

66 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace NetWorkHelper
{
/// <summary>
/// 负责将对象序列化成byte[]数组
/// </summary>
public class SerHelper
{
/// <summary>
/// 将实体序列化成byte[]数组
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] Serialize(object obj)
{
// 1.0 实例化流的序列化帮助类
BinaryFormatter bf = new BinaryFormatter();
//2.0 定义内存流用来接收通过bf的Serialize方法将obj对象序列化成byte[]字节数组
byte[] res;
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
res = ms.ToArray();
}
return res;
}
/// <summary>
/// 根据byte[]字节数组反序列化成 对象实体
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static T Deserialize<T>(byte[] buffer)
{
// 1.0 实例化流的序列化帮助类
BinaryFormatter bf = new BinaryFormatter();
T obj;
using (MemoryStream ms = new MemoryStream(buffer))
{
obj = (T)bf.Deserialize(ms);
}
//bf.Deserialize(
return obj;
}
/// <summary>
/// 根据byte[]字节数组反序列化成 对象实体
/// </summary>
/// <param name="datas"></param>
/// <param name="index"></param>
/// <returns></returns>
public static object Deserialize(byte[] datas, int index)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream(datas, index, datas.Length - index);
object obj = bf.Deserialize(stream);
stream.Dispose();
return obj;
}
}
}