、Binary方法进行序列化
1、首先。你被序列化的类需要用[Serializable]特性修饰,例如:
[Serializable]
public class AA
{
public int i = 5;
}
2、那么你可以用如下方法序列化和反序列化:
///
/// 序列化为二进制字节数组
///
/// 要序列化的对象
///
public static byte[] SerializeBinary
{
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
{
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 p2 = DeserializeBinary
二、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
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}
}
public static T[] Deserial
{
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;
}
}
}
ArrayList本身已经实现了序列化接口
、Binary方法进行序列化
1、首先。你被序列化的类需要用[Serializable]特性修饰,例如:
[Serializable]
public class AA
{
public int i = 5;
}
2、那么你可以用如下方法序列化和反序列化:
///
/// 序列化为二进制字节数组
///
/// 要序列化的对象
///
public static byte[] SerializeBinary
{
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
{
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 p2 = DeserializeBinary
二、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
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}
}
public static T[] Deserial
{
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;
}
}
}