原文:http://stackoverflow.com/questions/302821/serialization-in-c-without-using-file-system
public static byte[] Serialize<T>(T t) {
IFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream()) {
formatter.Serialize(stream, t);
return stream.ToArray();
}
}
public static T Deserialize<T>(byte[] b) {
IFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream(b)) {
return (T)formatter.Deserialize(stream);
}
}