如何序列化与反序列化复杂对象

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

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

 

None.gif using  System;
None.gif
using  System.Runtime.Serialization;
None.gif
using  System.Runtime.Serialization.Formatters.Binary;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  SGSoft.Asst
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// AsstTable 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Serializable()]
InBlock.gif    
public class AsstTable: ISerializable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
public AsstTable()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.mInterval=0;
InBlock.gif            
this.mRank=33;
InBlock.gif            mExpre_date
=System.DateTime.Now;
InBlock.gif            mLastEdit_time
=System.DateTime.Now;
InBlock.gif            mPriority
=1.0;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public AsstTable(int mInterval,uint mRank)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.mInterval=mInterval;
InBlock.gif            
this.mRank=mRank;
InBlock.gif            
this.mData=new int[mRank,mRank];
InBlock.gif            mExpre_date
=System.DateTime.Now.Date.AddDays(30);
InBlock.gif            mLastEdit_time
=System.DateTime.Now;
InBlock.gif
InBlock.gif            mTableName
=string.Format("matrix{0}",mInterval);
InBlock.gif
InBlock.gif            mDescription
=string.Format("粘网矩阵{0},创建间隔为{1};创建时间{2}",mTableName,
InBlock.gif                mInterval,mLastEdit_time.ToShortDateString());
InBlock.gif            mPriority
=1/mInterval;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 反序列化构照函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="info"></param>
ExpandedSubBlockEnd.gif        
/// <param name="ctxt"></param>

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

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有成员#region 私有成员
InBlock.gif        
//间隔
InBlock.gif
        private int mInterval;        
InBlock.gif        
//矩阵的秩
InBlock.gif
        private readonly uint mRank;
InBlock.gif        
//说明信息
InBlock.gif
        private string mDescription=null;
InBlock.gif        
//过期时间
InBlock.gif
        private System.DateTime mExpre_date;
InBlock.gif        
//最后编辑时间
InBlock.gif
        private System.DateTime mLastEdit_time;
InBlock.gif        
//表名
InBlock.gif
        private string mTableName=null;
InBlock.gif        
//表ID
InBlock.gif        
InBlock.gif        
//表的权
InBlock.gif
        private double mPriority;
InBlock.gif
InBlock.gif        
//粘网矩阵
InBlock.gif
        private int[,] mData=null;
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
//公共属性#region        //公共属性
InBlock.gif
InBlock.gif        
public int Interval
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mInterval;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                mInterval
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Property Rank (uint)
InBlock.gif        
/// 只读属性
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint Rank
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.mRank;
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public int[,] Data
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return mData;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
             
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Property Description (string)
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Description
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.mDescription;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.mDescription = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Property TableName (string)
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string TableName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.mTableName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.mTableName = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Property Priority (float)
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public double Priority
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.mPriority;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.mPriority = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
ISerializable 成员#region ISerializable 成员
InBlock.gif
InBlock.gif        
public void GetObjectData(SerializationInfo info, StreamingContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            info.AddValue(
"mInterval",mInterval);
InBlock.gif            info.AddValue(
"mRank",mRank);
InBlock.gif            info.AddValue(
"mDescription",mDescription);
InBlock.gif            info.AddValue(
"mExpre_date",mExpre_date);
InBlock.gif            info.AddValue(
"mLastEdit_time",mLastEdit_time);
InBlock.gif            info.AddValue(
"mPriority",mPriority);
InBlock.gif
InBlock.gif            
for(int i=0;i<mRank;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int j=0;j<mRank;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    info.AddValue(
string.Format("{0}-{1}",i,j),mData[i,j]);
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

下面是测试它的类:

 

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  NUnit.Framework;
None.gif
using  SGSoft.Asst;
None.gif
None.gif
using  System.IO;
None.gif
None.gif
namespace  TestProject
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Test 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif        [Test,Ignore(
"OK")]
InBlock.gif        
public void TestAsst()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                        
InBlock.gif            AsstTable at
=new AsstTable(1,33);
InBlock.gif
InBlock.gif            
int[,] ss=at.Data;
InBlock.gif            
for(int i=0;i<at.Rank;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int j=0;j<at.Rank;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ss[i,j]
=i+j;
InBlock.gif                    Console.WriteLine(ss[i,j]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            at.Description
="我爱你";
InBlock.gif            at.Priority
=1.1;
InBlock.gif            at.TableName
="Tablt1";
InBlock.gif            
InBlock.gif
InBlock.gif            System.IO.Stream sm
=File.Open("C:\\tests.txt",FileMode.Create);
InBlock.gif            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
InBlock.gif            bf.Serialize(sm,at);
InBlock.gif            sm.Close();
InBlock.gif
InBlock.gif            
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public void TestDeS()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.IO.Stream sm
=File.Open("C:\\tests.txt",FileMode.Open);
InBlock.gif            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
InBlock.gif            AsstTable at
=(AsstTable)bf.Deserialize(sm);
InBlock.gif            sm.Close();
InBlock.gif
InBlock.gif            Console.WriteLine(at.Description);
InBlock.gif            Console.WriteLine(at.Priority);
InBlock.gif
InBlock.gif            Console.WriteLine(at.Data[
22,4]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


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

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

e-Mail:     SGSoft@gmail.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值