C # getting started with serialization

Source: Internet
Author: User
Code
1 /**/ /* *************** Serialization and deserialization ***************
2 * Good text: Http://www.cnblogs.com/chjw8016/archive/2008/02/23/1078204.html
3 * 1. The process of converting an object into a byte sequence is called object serialization.
4 * 2. The process of restoring a byte sequence to an object is called object deserialization.
5 * 3. The simplest method is to mark the class using the serializable attribute.
6 * 4. iformatter provides serialized interfaces.
7 **************************************** ******* */
8 Using System;
9 Using System. Data;
10 Using System. configuration;
11 Using System. Web;
12 Using System. Web. Security;
13 Using System. Web. UI;
14 Using System. Web. UI. webcontrols;
15 Using System. Web. UI. webcontrols. webparts;
16 Using System. Web. UI. htmlcontrols;
17
18 /**/ /// 
19///Summary of myobject
20/// 
21 [Serializable]
22 Public   Class Myobject
23 {
24 Public   Int N1 =   0 ;
25 Public   Int N2 =   0 ;
26 Public   String Str =   Null ;
27   Public Myobject ()
28   {
29//
30//Todo: add the constructor logic here
31//
32}
33 }
34 Default. CS
35
36 Using System;
37 Using System. Data;
38 Using System. configuration;
39 Using System. Web;
40 Using System. Web. Security;
41 Using System. Web. UI;
42 Using System. Web. UI. webcontrols;
43 Using System. Web. UI. webcontrols. webparts;
44 Using System. Web. UI. htmlcontrols;
45
46 Using System. IO;
47 Using System. runtime. serialization;
48 Using System. runtime. serialization. formatters. Binary;
49
50 Public   Partial   Class _ Default: system. Web. UI. Page
51 {
52 Protected   Void Page_load ( Object Sender, eventargs E)
53 {
54//Funcserialize ();
55Funcdeserialize ();
56}
57 /**/ /// 
58///Serialization: serializes an object into a file.
59/// 
60 Private   Void Funcserialize ()
61 {
62 Myobject OBJ =   New Myobject ();
63 OBJ. N1 =   1 ;
64 OBJ. N2 =   24 ;
65 OBJ. Str =   " String " ;
66 Iformatter formatter =   New Binaryformatter ();
67 Stream stream =   New Filestream ( @" C: \ myfile. Bin " , Filemode. Create, fileaccess. Write, fileshare. None );
68 Formatter. serialize (stream, OBJ );
69 Stream. Close ();
70 }
71 /**/ /// 
72///Deserialization: convert a file into an object
73/// 
74 Private   Void Funcdeserialize ()
75 {
76 Iformatter formatter =   New Binaryformatter ();
77 Stream stream =   New Filestream ( @" C: \ myfile. Bin " , Filemode. Open,
78 Fileaccess. Read, fileshare. Read );
79 Myobject OBJ = (Myobject) formatter. deserialize (Stream );
80 Stream. Close ();
81 This . Title = OBJ. STR;
82 }
83 }
84

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.