C # serialization of objective Unit functions

Source: Internet
Author: User
1. What is a serialized object?
Serialization is the process of converting the object state to a format that can be maintained or transmitted.
During serialization, the object writes its current state to the temporary or persistent storage area. Later, you can re-create the object by reading or deserializing the object status from the bucket. In contrast to serialization, deserialization converts a stream into an object. By combining these two processes, data can be easily stored and transmitted.
Specifically, it should be the serialization of objects. Program During runtime, the generated objects disappear as the program stops running, but if we want to save some objects (because they have different features, after the program stops running, these objects still exist. You can read the values of these objects when the program runs again, or use these stored objects in other programs. In this case, Object serialization is required.

The system. runtime. serialization and system. runtime. serialization. formatters naming spaces provide the basic structure of the serialized object. These two types are actually the basic architecture.

2. There are two available implementation methods in the framework:
System. runtime. serialization. formatters. binary: This namespace also contains the binaryformatter class. It can serialize an object to a binary number and serialize the binary number to an object.
System. runtime. serialization. formatters. soap: This namespace contains the soapformatter class. It can serialize the object to XML data in the soap format and convert the XML data sequence in the soap format into an object.

Iii. Example:
1. A new windows application is added. form1 has two textbox controls.
Note: TXB: results produced by product products after the metric value is used
Txbresult: used to show the result after deserialization
2. Add a product type to format the input values of the queue.
3. serialize the results of products in applications

Generation:
Generation of products: Namespace Serializerfile
{
///   <Summary>
/// This type is used for formatting the input value of a pair.
/// Serializable: This mark can be serialized
///   </Summary>
[Serializable]
Class Product
{
Public   Long Lid;
Public   String Sname;
Public   Double Dprice;

/// <Summary>
///This mark cannot be serialized.
/// </Summary>
[Nonserialized]
StringSnotes;

Public Product ( Long Alid, String Asname, Double Adprice, String Asnotes)
{
Lid = Alid;
Sname = Asname;
Dprice = Adprice;
Snotes = Asnotes;
}

Public   Override   String Tostring ()
{
// Format the value of the delimiter.
Return   String . Format ( " {0 }:{ 1} ($ {2: F2}) {3} " , Lid, sname, dprice, snotes );
}
}
}

Generation in form1:

Using System. runtime. serialization;
Using System. runtime. serialization. formatters. Binary;
Using System. IO;

Namespace Serializerfile
{
Public   Partial   Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
// Serialize Method
Serializationclass ();
}

///   <Summary>
/// Serialization Method
///   </Summary>
Private   Void Serializationclass ()
{
List < Product > Products =   New List < Product > ();
// Generate value
Products. Add ( New Product ( 1 , " Spiky pung " , 1000.00 , " Good stuff. " ));
Products. Add ( New Product ( 2 , " Gloop galloop soup " , 25.0 , " Tasty. " ));
Products. Add ( New Product ( 4 , " Hat sauce " , 12.0 , " One for the kids. " ));

// For convenience, the generated values are displayed on the interface.
Foreach (Product pproduct In Products)
{
TXB. Text + = Pproduct. tostring () + " \ B \ r " ;
}

// define the serializable interface
iformatter ifserializer = New binaryformatter ();

filestream fssavefile = New filestream ( @" D: \ products. bin " , filemode. create, fileaccess. write);
/// serialization (sequence the content in products to the file D: \ products. BIN)
ifserializer. serialize (fssavefile, products);
fssavefile. close ();

Filestream fdloadfile =   New Filestream ( @" D: \ products. Bin " , Filemode. Open, fileaccess. Read );
// Deserialization
List < Product > Lsaveproducts = Ifserializer. deserialize (fdloadfile) As List < Product > ;
Fdloadfile. Close ();
// The result of deserialization is displayed on the interface.
Foreach (Product pproduct In Lsaveproducts)
{
Txbresult. Text + = Pproduct. tostring () + " \ B \ r " ;
}
}
}
}

The result is as follows:

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.