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

2024-11-26 02:40:26
推荐回答(3个)
回答1:

、Binary方法进行序列化
1、首先。你被序列化的类需要用[Serializable]特性修饰,例如:
[Serializable]
public class AA
{
public int i = 5;
}
2、那么你可以用如下方法序列化和反序列化:
///


/// 序列化为二进制字节数组
///

/// 要序列化的对象
/// 字节数组
public static byte[] SerializeBinary(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();
}
///
/// 从二进制数组反序列化得到对象
///

/// 字节数组
/// 得到的对象
public static T DeserializeBinary(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(p);
AA p2 = DeserializeBinary(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[] 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(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;
}
}
}

回答2:

ArrayList本身已经实现了序列化接口

回答3:

、Binary方法进行序列化
1、首先。你被序列化的类需要用[Serializable]特性修饰,例如:
[Serializable]
public class AA
{
public int i = 5;
}
2、那么你可以用如下方法序列化和反序列化:
///


/// 序列化为二进制字节数组
///

/// 要序列化的对象
/// 字节数组
public static byte[] SerializeBinary(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();
}
///
/// 从二进制数组反序列化得到对象
///

/// 字节数组
/// 得到的对象
public static T DeserializeBinary(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(p);
AA p2 = DeserializeBinary(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[] 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(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;
}
}
}