///<summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
/// <returns>返回存放序列化后的数据缓冲区</returns>
public static byte[] Serialize(object data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream mems = new MemoryStream();
formatter.Serialize(mems, data);
return mems.GetBuffer();
}
查阅MSDN发现原来是需要序列化的class的定义需要mark it with the Serializable attribute。
[Serializable]
public class MyObject {
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
附上MSDN关于序列化的几点建议大致意思:
确定一个class是否要定义为serializable 应该思考几个问题:该类是否有夸应用程序使用的需求?是否可能被远程使用(通过socket发送? By Youqi.)?该类的派生类是否有可能需要被序列化呢?等等。如果不确定就建议用serializable修饰,除非有以下下情况:
2.如果包含只有在当前这一个实例中有效的特殊的成员(unmanaged memory or file handles),可用NonSerialized 修饰,实例化过程中将忽略该元素;
3.如果类中数据成员包含敏感信息,需要有选择性的对成员进行序列化,建议implement ISerializable 来实现,做法更灵活。
2、使用XmlSerializer进行串行化
关于格式化器还有一个问题,假设我们需要XML,有两中方案:要么编写一个实现IFormatter接口的类,采用的方式类似于SoapFormatter类,但是没有你不需要的信息;要么使用库类XmlSerializer,这个类不使用Serializable属性,但是它提供了类似的功能。
如果我们不想使用主流的串行化机制,而想使用XmlSeralizer进行串行化我们需要做一下修改:
a.添加System.Xml.Serialization命名空间。
b.Serializable和NoSerialized属性将被忽略,而是使用XmlIgnore属性,它的行为与NoSerialized类似。
c.XmlSeralizer要求类有个默认的构造器,这个条件可能已经满足了。
序列化:
XmlSerializer xs = new XmlSerializer(typeof(List<Peoson>));
xs.Serialize(fs, listPers);
反序列化:
XmlSerializer xs = new XmlSerializer(typeof(List<Peoson>));
List<Peoson> list = xs.Deserialize(fs) as List<Peoson>;
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); this.pictureBox1.Image 二. C#中byte[]与string的转换代码 1、System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] inputBytes =converter.GetBytes(inputString); string inputString = converter.GetString(inputBytes); 2、string inputString = System.Convert.ToBase64String(inputBytes); byte[] inputBytes = System.Convert.FromBase64String(inputString); FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); 三. C# Stream 和 byte[] 之间的转换 /// 将 Stream 转成 byte[] public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; } /// 将 byte[] 转成 Stream public Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(bytes); return stream; } 四. Stream 和 文件之间的转换 将 Stream 写入文件 public void StreamToFile(Stream stream,string fileName) { // 把 Stream 转换成 byte[] byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); // 把 byte[] 写入文件 FileStream fs = new FileStream(fileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); } 五. 从文件读取 Stream public Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); // 把 byte[] 转换成 Stream Stream stream = new MemoryStream(bytes); return stream; }
原文:https://www.cnblogs.com/wwwbdabc/p/10756013.html