Binaryformatter serialization instance (4)

Source: Internet
Author: User
In C #, everything is an object, and all classes are derived from system. object. All array instances are objects, and all array classes are derived from system. array. The array class is serializable, so can voucher [] be directly serialized? We can try another method to serialize multiple voucher, Code As follows:
Public   Void Batchserialize ( String Filename, voucher [] vouchers)

{

Binaryformatter formatter= NewBinaryformatter ();

Filestream FS= NewFilestream (filename, filemode. Create );


Formatter. serialize (FS, vouchers );

FS. Close ();

}


Public Voucher [] batchdeserialize ( String Filename)

{

Binaryformatter formatter =   New Binaryformatter ();

Voucher [] vouchers =   Null ;


Filestream FS =   New Filestream (filename, filemode. Open );



Try

{

Vouchers=(Voucher []) formatter. deserialize (FS );

}

Finally

{

FS. Close ();

}


Return Vouchers;

}

Because the interface has not changed, you can use the nunit test method to pass the test. Therefore, we can draw a conclusion,If Class A can be serialized, a [] can also be serialized.



The nunit Testing Method serializes voucher [] to generate the following binary file:




To compare the two serialization methods, the file content generated by serializing multiple objects is listed below:




The file generated by serializing an array is much smaller than the file generated by serializing multiple objects in sequence. When arrays are serialized, the assembly information, voucher class information, and voucher fields information only needs to be output once. When multiple objects are serialized in sequence, the corresponding information should be output for each object.

In addition to the type information, there is an interesting phenomenon: "2005012900001" and "xingd" are output only once during array serialization, but two voucher objects are generated in the nunit test, although their voucherid and creator values are the same .. Is output only once because the values are the same. Modify the test method as follows:

[Test]

Public   Void Testserializevouchers ()

{

Voucher [] vouchers =   New Voucher [] { New voucher ( " 2005012900001 " , " xingd " , datetime. now), New voucher ( " 2005012900002 " , " Steven " , datetime. now) } ;


Voucherserializer serializer =   New Voucherserializer ();

Serializer. batchserialize ( " Voucher. dat " , Vouchers );

}

The output vouhcer. dat content is as follows:






We can see that both voucherid and creator of the two voucher objects have output. However, in the two test methods, the createdtime of both voucher objects is output separately, but they should actually have the same value. So, is it because datetime is a value type, while string is a reference type. When processing strings in. Net, a built-in string pool is generated. strings with the same value will reference the same object in the value. To learn more, modify the Test method:

[Test]

Public   Void Testserializevouchers ()

{

Voucher voucher =   New Voucher ( " 2005012900001 " , " Xingd " , Datetime. Now );

Voucher [] vouchers =   New Voucher [] {Voucher, voucher} ;


Voucherserializer serializer =   New Voucherserializer ();

Serializer. batchserialize ( " Voucher. dat " , Vouchers );

}

The generated file is as follows:

obviously, only one voucher object produces output. During the binaryformatter serialization process, it seems that the entire graph of the object to be serialized is generated first, and then the objects involved are serialized one by one. Therefore, not only does the same object in the array avoid repeated output, but in the graph formed by the object referenced by the serialized object, the same object will only be output once.

the comparison in this article determines that the overall serialization of the Collection class should be given priority. , instead of serializing each element in the set.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.