C#二进制流的序列化和反序列化操作
C#项目中较多使用了序列化和反序列化,较为常用的序列化和反序列化操作有二进制流,JSON,XML等,现在介绍一下.net中二进制流的序列化和反序列化操作方法:
1.将对象序列化为二进制流:
///<summary>///将对象序列化为byte[]///使用IFormatter的Serialize序列化///</summary>///<paramname="obj">需要序列化的对象</param>///<returns>序列化获取的二进制流</returns>publicstaticbyte[]FormatterObjectBytes(objectobj){if(obj==null)thrownewArgumentNullException("obj");byte[]buff;try{using(varms=newMemoryStream()){IFormatteriFormatter=newBinaryFormatter();iFormatter.Serialize(ms,obj);buff=ms.GetBuffer();}}catch(Exceptioner){thrownewException(er.Message);}returnbuff;}
2.将对象转为二进制文件,并保存到指定的文件中:
///<summary>///将对象转为二进制文件,并保存到指定的文件中///</summary>///<paramname="name">文件路径</param>///<paramname="obj">待存的对象</param>///<returns></returns>publicstaticboolBinaryFileSave(stringname,objectobj){Streamflstr=null;BinaryWriterbinaryWriter=null;try{flstr=newFileStream(name,FileMode.Create);binaryWriter=newBinaryWriter(flstr);varbuff=FormatterObjectBytes(obj);binaryWriter.Write(buff);}catch(Exceptioner){thrownewException(er.Message);}finally{if(binaryWriter!=null)binaryWriter.Close();if(flstr!=null)flstr.Close();}returntrue;}
3.将byte[]反序列化为对象:
///<summary>///将byte[]反序列化为对象///使用IFormatter的Deserialize发序列化///</summary>///<paramname="buff">传入的byte[]</param>///<returns></returns>publicstaticobjectFormatterByteObject(byte[]buff){if(buff==null)thrownewArgumentNullException("buff");objectobj;try{using(varms=newMemoryStream()){IFormatteriFormatter=newBinaryFormatter();obj=iFormatter.Deserialize(ms);}}catch(Exceptioner){thrownewException(er.Message);}returnobj;}
4.将对象序列化为byte[]:
///<summary>///将对象序列化为byte[]///使用Marshal的StructureToPtr序列化///</summary>///<paramname="obj">需序列化的对象</param>///<returns>序列化后的byte[]</returns>publicstaticbyte[]MarshalObjectByte(objectobj){if(obj==null)thrownewArgumentNullException("obj");byte[]buff;try{buff=newbyte[Marshal.SizeOf(obj)];varptr=Marshal.UnsafeAddrOfPinnedArrayElement(buff,0);Marshal.StructureToPtr(obj,ptr,true);}catch(Exceptioner){thrownewException(er.Message);}returnbuff;}
5.将byte[]序列化为对象:
///<summary>///将byte[]序列化为对象///</summary>///<paramname="buff">被转换的二进制流</param>///<paramname="type">转换成的类名</param>///<returns></returns>publicstaticobjectMarshalByteObject(byte[]buff,Typetype){if(buff==null)thrownewArgumentNullException("buff");if(type==null)thrownewArgumentNullException("type");try{varptr=Marshal.UnsafeAddrOfPinnedArrayElement(buff,0);returnMarshal.PtrToStructure(ptr,type);}catch(Exceptioner){thrownewException(er.Message);}}
6.将文件转换为byte数组:
///<summary>///将文件转换为byte数组///</summary>///<paramname="path">文件地址</param>///<returns>转换后的byte[]</returns>publicstaticbyte[]FileObjectBytes(stringpath){if(string.IsNullOrEmpty(path))thrownewArgumentNullException("path");if(!File.Exists(path))returnnewbyte[0];try{varfi=newFileInfo(path);varbuff=newbyte[fi.Length];varfs=fi.OpenRead();fs.Read(buff,0,Convert.ToInt32(fs.Length));fs.Close();returnbuff;}catch(Exceptioner){thrownewException(er.Message);}}
7.将byte[]转换为文件并保存到指定的地址:
///<summary>///将byte[]转换为文件并保存到指定的地址///</summary>///<paramname="buff">需反序列化的byte[]</param>///<paramname="savePath">文件保存的路径</param>///<returns>是否成功</returns>publicstaticstringFileByteObject(byte[]buff,stringsavePath){if(buff==null)thrownewArgumentNullException("buff");if(savePath==null)thrownewArgumentNullException("savePath");if(File.Exists(savePath))return"文件名重复";try{varfs=newFileStream(savePath,FileMode.CreateNew);varbw=newBinaryWriter(fs);bw.Write(buff,0,buff.Length);bw.Close();fs.Close();}catch(Exceptioner){thrownewException(er.Message);}return"保存成功";}
8.将图片序列化为二进制流:
///<summary>///将图片序列化为二进制流///</summary>///<paramname="imgPath">图片路径</param>///<returns>序列化后的二进制流</returns>publicstaticbyte[]SetImgToBytes(stringimgPath){if(string.IsNullOrEmpty(imgPath))thrownewArgumentNullException(imgPath);try{byte[]byteData;using(varfile=newFileStream(imgPath,FileMode.Open,FileAccess.Read)){byteData=newbyte[file.Length];file.Read(byteData,0,byteData.Length);file.Close();}returnbyteData;}catch(Exceptioner){thrownewException(er.Message);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。