Isabella
---- I fell upon the thorn of life, I bleed.
  转自:http://sgsoft.cnblogs.com/archive/2004/10/10/50534.html

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

下面是我写的一个软件中的部分有关序列化的代码,共享之供大家批评,这里展示的是简单的二进制序列化,复杂的还有自定义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));
                }

            }


        }

        
私有成员#region 私有成员
        
//间隔
        private int mInterval;        
        
//矩阵的秩
        private readonly uint mRank;
        
//说明信息
        private string mDescription=null;
        
//过期时间
        private System.DateTime mExpre_date;
        
//最后编辑时间
        private System.DateTime mLastEdit_time;
        
//表名
        private string mTableName=null;
        
//ID
        
        //
表的权
        private double mPriority;

        
//粘网矩阵
        private int[,] mData=null;

        
#endregion

        
//公共属性#region        //公共属性

        
public int Interval
        
{
            
get
            
{
                
return mInterval;
            }
            
set
            
{
                mInterval=value;
            }
        }

        
/**//// <summary>
        
/// Property Rank (uint)
        
/// 只读属性
        
/// </summary>
        public uint Rank
        
{
            
get
            
{
                
return this.mRank;
            }            
        }


        
public int[,] Data
        
{
            
get
            
{
                
return mData;
            }
        }             

        
/**//// <summary>
        
/// Property Description (string)
        
/// </summary>
        public string Description
        
{
            
get
            
{
                
return this.mDescription;
            }
            
set
            
{
                
this.mDescription = value;
            }
        }

        
/**//// <summary>
        
/// Property TableName (string)
        
/// </summary>
        public string TableName
        
{
            
get
            
{
                
return this.mTableName;
            }
            
set
            
{
                
this.mTableName = value;
            }
        }


        
/**//// <summary>
        
/// Property Priority (float)
        
/// </summary>
        public double Priority
        
{
            
get
            
{
                
return this.mPriority;
            }
            
set
            
{
                
this.mPriority = value;
            }
        }

        
#endregion
        
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
    }
}


 

下面是测试它的类:

 

using System;
using System.Collections;
using NUnit.Framework;
using SGSoft.Asst;

using System.IO;

namespace TestProject
{
    
/**//// <summary>
    
/// Test 
的摘要说明。
    
/// </summary>
    [TestFixture]
    
public class Test
    
{
        
public Test()
        
{
            
//
            // TODO: 
在此处添加构造函数逻辑
            //
        }
        [Test,Ignore("OK")]
        
public void TestAsst()
        
{
                        
            AsstTable at=
new AsstTable(1,33);

            
int[,] ss=at.Data;
            
for(int i=0;i<at.Rank;i++)
            
{
                
for(int j=0;j<at.Rank;j++)
                
{
                    ss[i,j]=i+j;
                    Console.WriteLine(ss[i,j]);
                }
            }

            at.Description="
我爱你";
            at.Priority=1.1;
            at.TableName="Tablt1";
            

            System.IO.Stream sm=File.Open("C:\\tests.txt",FileMode.Create);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            bf.Serialize(sm,at);
            sm.Close();

            


        }


        
public void TestDeS()
        
{
            System.IO.Stream sm=File.Open("C:\\tests.txt",FileMode.Open);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            AsstTable at=(AsstTable)bf.Deserialize(sm);
            sm.Close();

            Console.WriteLine(at.Description);
            Console.WriteLine(at.Priority);

            Console.WriteLine(at.Data[22,4]);
        }



    }
}


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

大家如果有对自定义XML序列化实现的较好的代码,希望能和我交流。

 

posted on 2007-02-15 16:35  李昀璟  阅读(212)  评论(0)    收藏  举报