Chapter 8th Flow and serialization

Source: Internet
Author: User

8.1 Files

8.1.1 files from different perspectives

Application level, human readable level, machine level

8.1.2-bit, byte and byte arrays

8.2 Streams

8.2.1 on the flow analogy

8.2.2 using streams for file copying

1. Copy Once

2. Cyclic partial replication

When you open or create a file, the stream pointer is in the file header by default, and when the read or write method is called, the stream pointer automatically moves the corresponding byte backwards. Therefore, there is no need to set up in the code, and each time you call read or write, the bytes that are read are unhandled.

The type system of 8.2.3 flow

1. Base Stream

Reading data from the stream: Canread;read;readbyte

Write data to the stream: Canwrite;write;writebyte

Move flow pointer: Canseek;seek;position;close;dispose;flush;

Timeout processing: cantimeout;readtimeout;writetimeout;

Flow length: Length;setlength

2. Adorner flow

1) contains the stream stream base class reference

2) No backup storage concept

Bufferedstream;deflatestream;gzipstream

3. Wrapper class

1) StreamReader and StreamWriter

2) BinaryReader and BinaryWriter

4. Help Class

File Static class: Open;openwrite;openread;readalltext;readallbytes;writeallbytes;writealllines;copy

Fileinfo;path;directory;dicrectoryinfo

8.3 Serialization

8.3.1 Basic Operation

Iformatter:serialize;deserialize

BinaryFormatter and SoapFormatter

Types are not serializable by default and need to be explicitly stated [Serializable] attribute (133)

Serialization requires not only that the type is marked as serializable, but also that the attributes and fields in the type need to be serializable. If serialization is not required, it is marked with the [NonSerialized] attribute, which can only be added to the field and cannot be added to the attribute (134).

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Collections;usingSystem.IO;usingSystem.Data.SqlClient;usingSystem.Runtime.Serialization;usingSystem.Runtime.Serialization.Formatters.Binary;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Product Product=NewProduct (188) {Price =4998.5F, Name ="Lumia 920" }; IFormatter Formatter=NewBinaryFormatter (); Stream FS= File.openwrite (@"C:\Users\Administrator\Desktop\product.obj"); Formatter.            Serialize (fs, product); Fs.            Dispose ();            Console.WriteLine (product); //IFormatter formatter = new BinaryFormatter (); //Stream fs = File.openread (@ "C:\Users\Administrator\Desktop\product.obj"); //Product Product = (product) formatter.            Deserialize (FS); //FS.            Dispose (); //Console.WriteLine (product);Console.read (); } [Serializable] Public classProduct:ideserializationcallback {Private intID;  Public stringName {Get;Set; }  Public DoublePrice {Get;Set; } [NonSerialized]PrivateSqlConnection Conn;  PublicProduct (intID) { This. ID =ID; Conn=NewSqlConnection (@"Data Source=.;i Nitial catalog=db; User Id=sa; Password=123"); }             Public Override stringToString () {return string. Format ("Id:{0},name:{1},price:{2},conn:{3}", This. ID, This. Name, This. Price, This. conn = =NULL?"NULL":"OBJECT"); }             Public voidOnDeserialization (Objectsender) {Conn=NewSqlConnection (@"Data Source=.;i Nitial catalog=db; User Id=sa; Password=123"); }        }    }}
View Code

8.3.2 Event Response

[OnSerializing]

[OnSerialized]

[OnDeserializing]

[OnDeserialized]

Four features correspond to four event methods

8.3.3 Customizing the serialization process

Chapter 8th Flow and serialization

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.