如何序列化/反序列化的ArrayList

 我来答
育知同创教育
2016-12-03 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
、Binary方法进行序列化
1、首先。你被序列化的类需要用[Serializable]特性修饰,例如:
[Serializable]
public class AA
{
public int i = 5;
}
2、那么你可以用如下方法序列化和反序列化:
/// <summary>
/// 序列化为二进制字节数组
/// </summary>
/// <param name="request">要序列化的对象 </param>
/// <returns>字节数组 </returns>
public static byte[] SerializeBinary<T>(T request)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
serializer.Serialize(memStream, request);
return memStream.GetBuffer();
}
/// <summary>
/// 从二进制数组反序列化得到对象
/// </summary>
/// <param name="buf">字节数组 </param>
/// <returns>得到的对象 </returns>
public static T DeserializeBinary<T>(byte[] buf)
{
System.IO.MemoryStream memStream = new MemoryStream(buf);
memStream.Position = 0;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
T newobj = (T)deserializer.Deserialize(memStream);
memStream.Close();
return newobj;
}
3、调用方法
AA p = new AA();
p.i = 15;
byte[] bytes = SerializeBinary<AA>(p);
AA p2 = DeserializeBinary<AA>(bytes);

二、XML序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace RegexActivator
{
public class ExtendMethods
{
public static void Serial<T>(T[] items, string path)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}
}
public static T[] Deserial<T>(string path)
{
if (!File.Exists(path)) return new T[0];
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
FileStream fs = new FileStream(path, FileMode.Open);
T[] items;
try
{
items = (T[])xmlSerializer.Deserialize(fs);
}
finally
{
fs.Close();
}
return items;
}
}
}
微言摆渡人
2016-12-03 · TA获得超过875个赞
知道小有建树答主
回答量:1059
采纳率:59%
帮助的人:248万
展开全部
ArrayList本身已经实现了序列化接口
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
惬意又清冽的松柏8224
2017-10-18 · TA获得超过522个赞
知道小有建树答主
回答量:781
采纳率:90%
帮助的人:518万
展开全部
、Binary方法进行序列化
1、首先。你被序列化的类需要用[Serializable]特性修饰,例如:
[Serializable]
public class AA
{
public int i = 5;
}
2、那么你可以用如下方法序列化和反序列化:
/// <summary>
/// 序列化为二进制字节数组
/// </summary>
/// <param name="request">要序列化的对象 </param>
/// <returns>字节数组 </returns>
public static byte[] SerializeBinary<T>(T request)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
serializer.Serialize(memStream, request);
return memStream.GetBuffer();
}
/// <summary>
/// 从二进制数组反序列化得到对象
/// </summary>
/// <param name="buf">字节数组 </param>
/// <returns>得到的对象 </returns>
public static T DeserializeBinary<T>(byte[] buf)
{
System.IO.MemoryStream memStream = new MemoryStream(buf);
memStream.Position = 0;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
T newobj = (T)deserializer.Deserialize(memStream);
memStream.Close();
return newobj;
}
3、调用方法
AA p = new AA();
p.i = 15;
byte[] bytes = SerializeBinary<AA>(p);
AA p2 = DeserializeBinary<AA>(bytes);

二、XML序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace RegexActivator
{
public class ExtendMethods
{
public static void Serial<T>(T[] items, string path)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}
}
public static T[] Deserial<T>(string path)
{
if (!File.Exists(path)) return new T[0];
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
FileStream fs = new FileStream(path, FileMode.Open);
T[] items;
try
{
items = (T[])xmlSerializer.Deserialize(fs);
}
finally
{
fs.Close();
}
return items;
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式