48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace COMCMS.Common
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ByteConvertHelper
|
|
{
|
|
/// <summary>
|
|
/// 将对象转换为byte数组
|
|
/// </summary>
|
|
/// <param name="obj">被转换对象</param>
|
|
/// <returns>转换后byte数组</returns>
|
|
public static byte[] Object2Bytes(object obj)
|
|
{
|
|
string json = JsonConvert.SerializeObject(obj);
|
|
byte[] serializedResult = Encoding.UTF8.GetBytes(json);
|
|
return serializedResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将byte数组转换成对象
|
|
/// </summary>
|
|
/// <param name="buff">被转换byte数组</param>
|
|
/// <returns>转换完成后的对象</returns>
|
|
public static object Bytes2Object(byte[] buff)
|
|
{
|
|
string json = Encoding.UTF8.GetString(buff);
|
|
return JsonConvert.DeserializeObject<object>(json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将byte数组转换成对象
|
|
/// </summary>
|
|
/// <param name="buff">被转换byte数组</param>
|
|
/// <returns>转换完成后的对象</returns>
|
|
public static T Bytes2Object<T>(byte[] buff)
|
|
{
|
|
string json = Encoding.UTF8.GetString(buff);
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
}
|
|
}
|