在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
https://www.cnblogs.com/wangbg/p/14020022.html https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-5.0 /// <summary>
/// 序列化
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private static byte[] Serialize(object obj)
{
if (obj == null)
return null;
using var memoryStream = new MemoryStream();
DataContractSerializer ser = new DataContractSerializer(typeof(object));
ser.WriteObject(memoryStream, obj);
var data = memoryStream.ToArray();
return data;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
private static T Deserialize<T>(byte[] data)
{
if (data == null)
return default(T);
using var memoryStream = new MemoryStream(data);
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, new XmlDictionaryReaderQuotas());
DataContractSerializer ser = new DataContractSerializer(typeof(T));
var result = (T)ser.ReadObject(reader, true);
return result;
}
|
请发表评论