C#中Font类的序列化

http://topic.csdn.net/t/20040920/16/3391265.html

 

代码
using   System; 
using   System.Runtime.Serialization.Formatters.Binary; 
using   System.Runtime.Serialization; 
namespace   HFSoft.Serialization 

///   <summary> 
///   对象序列化对象类 
///   </summary> 
public   class   Serializable 

private   Serializable() 

// 
//   TODO:   在此处添加构造函数逻辑 
// 

///   <summary> 
///   把对象序列化并返回相应的字节 
///   </summary> 
///   <param   name= "pObj "> 需要序列化的对象 </param> 
///   <returns> byte[] </returns> 
public   static   byte[]   SerializeObject(object   pObj) 

if(pObj   ==   null
return   null
System.IO.MemoryStream   _memory   
=   new   System.IO.MemoryStream(); 
BinaryFormatter   formatter   
=   new   BinaryFormatter(); 
formatter.Serialize(_memory,pObj); 
_memory.Position   
=   0
byte[]   read   =   new   byte[_memory.Length]; 
_memory.Read(read,
0,read.Length); 
_memory.Close(); 
return   read; 



public   static   void   SerializeObject(object   pObj,string   pFileName) 

System.IO.FileStream   stream   
=   System.IO.File.Open(pFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write); 
byte[]   bytes   =   SerializeObject(pObj); 
stream.Write(bytes,
0,bytes.Length); 
stream.Flush(); 
stream.Close(); 

public   static   object   DeserializeObject(string   pFileName) 

System.IO.FileStream   stream   
=   System.IO.File.Open(pFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read); 
byte[]   bytes   =   new   byte[stream.Length]; 
stream.Read(bytes,
0,bytes.Length); 
stream.Close(); 
return   DeserializeObject(bytes); 

///   <summary> 
///   把字节反序列化成相应的对象 
///   </summary> 
///   <param   name= "pBytes "> 字节流 </param> 
///   <returns> object </returns> 
public   static   object   DeserializeObject(byte[]   pBytes) 

object   _newOjb   =   null
if(pBytes   ==   null
return   _newOjb; 
System.IO.MemoryStream   _memory   
=   new   System.IO.MemoryStream(pBytes); 
_memory.Position   
=   0
BinaryFormatter   formatter   
=   new   BinaryFormatter(); 
_newOjb   
=   formatter.Deserialize(_memory); 
_memory.Close(); 
return   _newOjb; 



 

 

posted @ 2011-02-12 22:39  yinwun  阅读(926)  评论(0)    收藏  举报