做推文封面的网站,搜索引擎是什么意思啊,jquery 网站源码,福州注册公司快点办关于C#序列化的文章真的是好多#xff0c;但是内容大致一样#xff0c;主要分四类#xff1a; BinarySerialize SoapSerialize XmlSerialize JSON.Net和DataContractJsonSerializer最近的一个项目需要使用Socket进行通信#xff0c;所以必然涉及序列化的问题。使用BinarySe… 关于C#序列化的文章真的是好多但是内容大致一样主要分四类 BinarySerialize SoapSerialize XmlSerialize JSON.Net和DataContractJsonSerializer 最近的一个项目需要使用Socket进行通信所以必然涉及序列化的问题。使用BinarySerialize序列化之后发现无论如何获取不了序列化后的实际长度代码如下using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;namespace Serialize
{public class BinarySerialize{byte[] buffernew byte[1024];public void Serialize(Book book){using (MemoryStream fs new MemoryStream(buffer, 0,1024)){BinaryFormatter formatter new BinaryFormatter();formatter.Serialize(fs, book);}}public Book DeSerialize(){Book book;using (MemoryStream fs new MemoryStream(buffer,0,1024)){BinaryFormatter formatter new BinaryFormatter();book (Book)formatter.Deserialize(fs);}return book;}}
} 通过MemoryStream获取长度永远是1024。后来试了好久发现其实只需要改一点点地方就可以了改正后的代码如下顺便做了点通用性扩展using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;namespace Serialize
{public class MemoryBinarySerialize{public static byte[] Serialize(object obj){MemoryStream fs new MemoryStream();BinaryFormatter formatter new BinaryFormatter();formatter.Serialize(fs, obj);return fs.GetBuffer();}public static object DeSerialize(byte[] data){MemoryStream fs new MemoryStream(data);BinaryFormatter formatter new BinaryFormatter();object obj formatter.Deserialize(fs);return obj;}}
} 参考文献 C#序列化技术详解转http://www.cnblogs.com/ejiyuan/archive/2009/01/21/1379256.html 序列化和反序列化Socket中传递Objecthttp://dev.firnow.com/course/4_webprogram/asp.net/netjs/20081011/150063.html C#中JSON的序列化和反序列化http://www.dotnetdev.cn/2010/01/c%E4%B8%AD%E7%9A%84json%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96/转载于:https://www.cnblogs.com/sdqxcxh/archive/2011/01/19/1939101.html