前几天写了一篇.NET序列化概述性的文章,有朋友说对技术的描述不够详细,因此准备写一系列文章,以一个实例为基础,详细描述各种序列化的使用方法和注意事项。这一系列文章是为不熟悉序列化的读者准备的,已用过序列化的可以只看一下每篇中加粗的一句话,感兴趣再阅读全文。文中的示例类Voucher和VoucherItem非常简化,实际中的凭证包含更多的信息。
  财务系统中经常要与凭证打交道,在这篇文章里,我们要实现一个Voucher类,它包含凭证和业务信息,并且能够保存为磁盘文件,以便于没有专用网络的分布式企业中,可以在终端输入凭证,然后通过拨号的方式将凭证批量更新到中心业务系统中。
  凭证中除了包含凭证号,制单人,制单时间等外,还包含多条项目。假设凭证项目也由其他人完成,以Assembly的方式提供,类名为VoucherItem,其定义如下:

BinaryFormatter序列化实例(一)public class VoucherItem
{

BinaryFormatter序列化实例(一)



BinaryFormatter序列化实例(一)}

  首先,我们要实现Voucher的主体类及其序列化,然后再把VoucherItem加到Voucher类上。
BinaryFormatter序列化实例(一)public class Voucher
{
BinaryFormatter序列化实例(一)    
private string voucherId;
BinaryFormatter序列化实例(一)    
private string creator;
BinaryFormatter序列化实例(一)    
private DateTime createTime;
BinaryFormatter序列化实例(一)    
private string description;
BinaryFormatter序列化实例(一)    
BinaryFormatter序列化实例(一)    
public Voucher(string voucherId, string creator, DateTime createTime)
{
BinaryFormatter序列化实例(一)        
this.voucherId = voucherId;
BinaryFormatter序列化实例(一)        
this.creator = creator;
BinaryFormatter序列化实例(一)        
this.createTime = createTime;
BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    
public string VoucherId
{
BinaryFormatter序列化实例(一)        
get
{
BinaryFormatter序列化实例(一)            
return voucherId;
BinaryFormatter序列化实例(一)        }

BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    
public string Creator
{
BinaryFormatter序列化实例(一)        
get
{
BinaryFormatter序列化实例(一)            
return creator;
BinaryFormatter序列化实例(一)        }

BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    
public DateTime CreateTime
{
BinaryFormatter序列化实例(一)        
get
{
BinaryFormatter序列化实例(一)            
return createTime;
BinaryFormatter序列化实例(一)        }

BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    
public string Description
{
BinaryFormatter序列化实例(一)        
get
{
BinaryFormatter序列化实例(一)            
return description;
BinaryFormatter序列化实例(一)        }

BinaryFormatter序列化实例(一)        
set
{
BinaryFormatter序列化实例(一)            description 
= value;
BinaryFormatter序列化实例(一)        }

BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)}


  为了将Voucher保存到磁盘,考虑最直接的方法是序列化,而为了通过拨号传到业务中心的需要,应当保存较小的磁盘文件,所以使用二进制序列化。
序列化代码如下:
BinaryFormatter序列化实例(一)public class VoucherSerializer
{
BinaryFormatter序列化实例(一)    
public VoucherSerializer()
{
BinaryFormatter序列化实例(一)        
BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    
public void Serialize(string filename, Voucher voucher)
{
BinaryFormatter序列化实例(一)        BinaryFormatter formatter 
= new BinaryFormatter();
BinaryFormatter序列化实例(一)        FileStream fs 
= new FileStream(filename, FileMode.Create);
BinaryFormatter序列化实例(一)        formatter.Serialize(fs, voucher);
BinaryFormatter序列化实例(一)        fs.Close();
BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)}

  从上面的代码可以看出,借助.NET Framework的帮助,实现类的二进制序列化是非常容易的。序列化方法写好后,马上用NUnit做一个测试类。

BinaryFormatter序列化实例(一)[TestFixture]
BinaryFormatter序列化实例(一)
public class VoucherTest
{
BinaryFormatter序列化实例(一)    
public VoucherTest()
{
BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)    [Test]
BinaryFormatter序列化实例(一)    
public void TestSerializeVoucher()
{
BinaryFormatter序列化实例(一)        Voucher voucher 
= new Voucher("2005012900001""xingd", DateTime.Now);
BinaryFormatter序列化实例(一)
BinaryFormatter序列化实例(一)        VoucherSerializer serializer 
= new VoucherSerializer();
BinaryFormatter序列化实例(一)        serializer.Serialize(
"voucher.dat", voucher);
BinaryFormatter序列化实例(一)    }

BinaryFormatter序列化实例(一)}


  兴冲冲地打开NUnit GUI,选中Assembly,运行测试。
  啊,怎么会有错误呢,这么简单的类。
GeneralLedger.VoucherTest.TestSerializeVoucher : System.Runtime.Serialization.SerializationException : The type GeneralLedger.Voucher in Assembly Voucher, Version=1.0.1855.38026, Culture=neutral, PublicKeyToken=null is not marked as serializable.

  原来是这个错。能够进行二进制序列化的类必须在类定义中添加SerializableAttribute。对Voucher类进行如下修改:

BinaryFormatter序列化实例(一)[Serializable]
BinaryFormatter序列化实例(一)
public class Voucher
  
  运行NUnit,测试通过,生成的文件内容如下: 
BinaryFormatter序列化实例(一)

  后面的随笔将会对此文件的格式进行分析。

相关文章:

  • 2021-06-24
  • 2022-02-24
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2021-07-26
  • 2022-02-13
  • 2022-02-07
猜你喜欢
  • 2021-11-15
  • 2022-03-05
  • 2021-12-13
  • 2021-07-03
  • 2021-08-21
  • 2022-02-07
相关资源
相似解决方案