昨夜飘风
昨 夜, 风, 飘 过; 枯 树, 叶, 飞 落。
 

对象序列化技术在大量数据缓存技术中需要用到,但对于复杂对象,如何实现序列化与反序列化呢?

下面是我写的一个软件中的部分有关序列化的代码,共享之供大家批评,这里展示的是简单的二进制序列化,复杂的还有自定义XML序列化,但由于本人对XML Schem不太熟悉,不知道如何编写高效的XML序列化,例如:Word可以直接存储为XML格式。
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;


namespace SGSoft.Asst
{
    
/**//// <summary>
    
/// AsstTable 的摘要说明。
    
/// </summary>

     [Serializable()]
    
public class AsstTable: ISerializable
    
{

        
public AsstTable()
        
{
            
this.mInterval=0;
            
this.mRank=33;
             mExpre_date
=System.DateTime.Now;
             mLastEdit_time
=System.DateTime.Now;
             mPriority
=1.0;

        }


        
/**//// <summary>
        
/// 客户调用时的构造函数
        
/// </summary>
        
/// <param name="mInterval">间隔,1表示相临</param>
        
/// <param name="mRank">数据表的秩,默认应该为32</param>

        public AsstTable(int mInterval,uint mRank)
        
{
            
this.mInterval=mInterval;
            
this.mRank=mRank;
            
this.mData=new int[mRank,mRank];
             mExpre_date
=System.DateTime.Now.Date.AddDays(30);
             mLastEdit_time
=System.DateTime.Now;

             mTableName
=string.Format("matrix{0}",mInterval);

             mDescription
=string.Format("粘网矩阵{0},创建间隔为{1};创建时间{2}",mTableName,
                 mInterval,mLastEdit_time.ToShortDateString());
             mPriority
=1/mInterval;

         }


        
/**//// <summary>
        
/// 反序列化构照函数
        
/// </summary>
        
/// <param name="info"></param>
        
/// <param name="ctxt"></param>

        public AsstTable(SerializationInfo info,StreamingContext ctxt)
        
{
             mInterval
=info.GetInt32("mInterval");
             mRank
=info.GetUInt32("mRank");
             mDescription
=info.GetString("mDescription");
             mExpre_date
=info.GetDateTime("mExpre_date");
             mLastEdit_time
=info.GetDateTime("mLastEdit_time");
             mPriority
=info.GetDouble("mPriority");
             mData
=new int[mRank,mRank];
            
for(int i=0;i<mRank;i++)
            
{
                
for(int j=0;j<mRank;j++)
                
{
                     mData[i,j]
=info.GetInt32(string.Format("{0}-{1}",i,j));
                 }


             }



         }


       
        ISerializable 成员#region ISerializable 成员

        
public void GetObjectData(SerializationInfo info, StreamingContext context)
        
{
             info.AddValue(
"mInterval",mInterval);
             info.AddValue(
"mRank",mRank);
             info.AddValue(
"mDescription",mDescription);
             info.AddValue(
"mExpre_date",mExpre_date);
             info.AddValue(
"mLastEdit_time",mLastEdit_time);
             info.AddValue(
"mPriority",mPriority);

            
for(int i=0;i<mRank;i++)
            
{
                
for(int j=0;j<mRank;j++)
                
{
                     info.AddValue(
string.Format("{0}-{1}",i,j),mData[i,j]);
                 }


             }


            
         }


        
#endregion

     }

}

该程序实现了完整的序列化与反序列化,当然,在流处理过程中,还可以使用Zip技术,直接对序列化流进行压缩。

posted on 2008-04-09 10:17  昨夜飘风  阅读(928)  评论(0编辑  收藏  举报